Strings in C

The way a group of integers can be stored in an integer array, similarly, a group of characters can be stored in a character array. Character arrays are many a time also called strings. A string is a group of characters, usually letters of the alphabet. Character arrays or strings are used by programming languages to manipulate text such as words and sentences.

A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ). For example,

char name[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0' } ; 

it would be stored in memory like below figure

strings representation in c

Points to Remember:-

  • Each character in the array occupies one byte of memory and the last character is always ‘\0’.
  • ‘\0’ is called a null character. Note that ‘\0’ and ‘0’ are not the same.
  • ASCII value of ‘\0’ is 0, whereas the ASCII value of ‘0’ is 48.
  • The terminating null (‘\0’) is important because it is the only way the functions that work with a string can know where the string ends. In fact, a string not terminated by a ‘\0’ is not really a string, but merely a collection of characters.

While entering the string using scanf( ) we must be cautious about two things:

The length of the string should not exceed the dimension of the character array. This is because the C compiler doesn’t perform bounds checking on character arrays. Hence, if you carelessly exceed the bounds there is always a danger of overwriting something important, and in that event, you would have nobody to blame but yourselves.

scanf( ) is not capable of receiving multi-word strings. Therefore names such as ‘Debashish Roy’ would be unacceptable. The way to get around this limitation is by using the function gets( ).

The usage of functions gets( ) and its counterpart puts( ) is shown below.

main( ) 
{ 
char name[25] ; 
printf ( "Enter your full name " ) ; 
gets ( name ) ; 
puts ( "Hello!" ) ; 
puts ( name ) ; 
} 

The program and the output are self-explanatory except for the fact that puts( ) can display only one string at a time (hence the use of two puts( ) in the program above). Also, on display a string, unlike printf( ), puts( ) places the cursor on the next line.

Standard Library String Functions

There are several built-in functions available in c some of them are:

strlen( ):

This function counts the number of characters present in a string. Its usage is illustrated in the following program.

main( ) 
{ 
char arr[ ] = "Bamboozled" ; 
int len1, len2 ;
len1 = strlen ( arr ) ; 
len2 = strlen ( "Humpty Dumpty" ) ; 
printf ( "\nstring = %s length = %d", arr, len1 ) ; 
printf ( "\nstring = %s length = %d", "Humpty Dumpty", len2 ) ; 
} 

The output would be…

string = Bamboozled length = 10 
string = Humpty Dumpty length = 13

strcpy( ):

This function copies the contents of one string into another. The base addresses of the source and target strings should be supplied to this function. Here is an example of strcpy( ) in action…

main( ) 
{ 
char source[ ] = "Sayonara" ;
char target[20] ; 
strcpy ( target, source ) ; 
printf ( "\nsource string = %s", source ) ; 
printf ( "\ntarget string = %s", target ) ; 
} 

And here is the output…

source string = Sayonara 
target string = Sayonara

strcat( ):

This function concatenates the source string at the end of the target string. For example, “Bombay” and “Nagpur” on concatenation would result in a string “BombayNagpur”. Here is an example of strcat( ) at work.

main( ) 
{ 
char source[ ] = "Folks!" ; 
char target[30] = "Hello" ; 
strcat ( target, source ) ; 
printf ( "\nsource string = %s", source ) ; 
printf ( "\ntarget string = %s", target ) ; 
} 

And here is the output…

source string = Folks! 
target string = HelloFolks!

strcmp( ):

This is a function that compares two strings to find out whether they are the same or different. The two strings are compared character by character until there is a mismatch or the end of one of the strings is reached, whichever occurs first. If the two strings are identical, strcmp( ) returns a value zero. If they’re not, it returns the numeric difference between the ASCII values of the first non-matching pairs of characters. Here is a program that puts strcmp( ) in action.

main( ) 
{ 
char string1[ ] = "Jerry" ; 
char string2[ ] = "Ferry" ; 
int i, j, k ; 
i = strcmp ( string1, "Jerry" ) ; 
j = strcmp ( string1, string2 ) ; 
k = strcmp ( string1, "Jerry boy" ) ; 
printf ( "\n%d %d %d", i, j, k ) ;
}

And here is the output…

0 4 -32

In the first call to strcmp( ), the two strings are identical—“Jerry” and “Jerry”—and the value returned by strcmp( ) is zero. In the second call, the first character of “Jerry” doesn’t match with the first character of “Ferry” and the result is 4, which is the numeric difference between the ASCII value of ‘J’ and the ASCII value of ‘F’. In the third call to strcmp( ) “Jerry” doesn’t match with “Jerry boy”, because the null character at the end of “Jerry” doesn’t match the blank in “Jerry boy”. The value returned is -32, which is the value of null character minus the ASCII value of space, i.e., ‘\0’ minus ‘ ’, which is equal to -32.