String Handling <string.h>

The string header file string.h contains many C string manipulation functions. You must include string.h first in the program in order to utilize these functions.


The strcpy function

The strcpy function copies the contents of one string (termed source) to another (termed destination) including the terminating null character. Its primary use is to assign one string to another.

The statement structure of the strcpy function is:

	char *strcpy( char *destination, char *source );
The * notations indicate that the strings are referred to by their addresses. The value returned is the address of the destination string.

Example:

	char   stringA[5] = "abcd";	// declare and initialize strings
	char   stringB[5];

	strcpy( stringB, stringA );	// assign the value of stringA to stringB

The strcat function

The strcat function adds a string to the end of another string. A terminating null character is added at the end.

The statement structure of the strcat function is:

	char *strcat( char *origin, char *add );
The * notations indicate that the strings are referred to by their addresses. The value returned is the address of the "concatenated" string. concatenate means attaching one string to the end of another string.

Examples:

	char   firstName[20] = "Bob";		// declare and initialize strings
	char   lastName[9] = "Jones";

	cout << strcat( firstName, lastName );	// output is "BobJones" 
	strcpy( firstName, "Bob" );		// assign "Bob" back to firstName
	strcat( firstName, " ");		// attach a blank
	cout << strcat( firstName, lastName )	// output is "Bob Jones" 

The strcmp function

The strcmp function compares the contents of two strings, character by character, from the first position to the end of the shorter string or till encountering first unequal pair of characters. Characters are compared by their ASCII values.

The ASCII order of characters are

Null (\0')
blank (\32') ! " # $ % & ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ? @
ABCDEFGHIJKLMNOPQRSTUVWXYZ [ \ ] ^ _`
abcdefghijklmnopqrstuvwxyz { | } ~
The statement structure of the strcmp function is:
	int strcmp( char *firstString, char *secondString );
The * notations indicate that the strings are referred to by their addresses. The value returned is:
0 - two strings are equal
1 - firstString is greater than secondString
-1 - firstString is less than secondString
Examples:
	char   stringA[5] = "abcd";	// declare and initialize strings
	char   stringB[5] = "abc";
	char   stringC[5] = "123a";
	char   stringD[5];

	strcpy( stringD, stringA );	// assign the value of stringA to stringD 
	if ( strcmp( stringA, stringB) > 0 )
	{
		cout<< "stringA is greater than stringB";
	}
	else if ( strcmp( stringA, stringB) == 0 )
	       {
		cout<< " stringA equals stringB";
	        }
	       else
	       {
		cout << " stringA is less than stringB";
	        }

	cout << strcmp( stringA, stringD );	// output is 0
	cout << strcmp( stringC, stringB );	// output is -1
	cout << strcmp( stringB, stringC );	// output is 1

The strlen function

The strlen function returns the length of its string argument.

The statement structure of the strlen function is:

	int strlen( char *cString );
The * notation indicates that the string is referred to by its address. The value returned is the number of characters in the string, not including the terminating null character.

Example:

	char name[6] = "Jones";	  // declare and initialize the string
	cout << strlen( name );	  // the result of strlen( name ) is 5