home
C TUTORIALS

Strings

The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.

The following declaration and initialization create a string consisting of the word "csec". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "csec"

char greeting[5] = {'c', 's', 'e', 'c', '\0'};


If you follow the rule of array initialization then you can write the above statement as follows:

char greeting[] = "csec";


Following is the memory presentation of above defined string in C/C++:

0 1 2 3 4
'c' 's' 'e' 'c' '/0'
4001 4002 4003 4004 4005


Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array. Let us try to print above mentioned string:

#include int main () { char greeting[5] = {'c', 's', 'e', 'c', '\0'}; printf("message: %s\n", greeting ); return 0; }


When the above code is compiled and executed, it produces result something as follows:

message: csec


It can also be demonstrated as:

main() { char name[]="CSEC"; int i=0; while(i<=3) { printf("%c",name[i]); i++; } }


Output:

csec


WE can also write is as

while(name[i]!='\0') { printf("%c",name[i]); i++; }


Another version for same program, this one uses a pointer to access the array elements.

main() { char name[]="csec"; char *ptr; ptr=name; /*store base address of string*/ while(*ptr!='\0') { printf("%c",*ptr); ptr++; } }


While entering the string using scanf() we must be cautious about two things:
(a) The length of the string should not exceed the dimension of the character array.
(b) scanf() is not capable of receiving multi-word strings. Therefore, names such as 'Nit hamirpur' would be unacceptable. The usage of gets() in place of scanf() and its countrpart puts() is done.

Standard Library String Functions

Function Use
strcpy(s1, s2); Copies string s2 into string s1.
strcat(s1, s2); Concatenates string s2 onto the end of string s1.
strlen(s1); Returns the length of string s1.
strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1s2
strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.


For example:

#include #include int main () { char str1[12] = "Hello"; char str2[12] = "World"; char str3[12]; int len ; /* copy str1 into str3 */ strcpy(str3, str1); printf("strcpy( str3, str1) : %s\n", str3 ); /* concatenates str1 and str2 */ strcat( str1, str2); printf("strcat( str1, str2): %s\n", str1 ); /* total lenghth of str1 after concatenation */ len = strlen(str1); printf("strlen(str1) : %d\n", len ); return 0; }


When the above code is compiled and executed, it produces result something as follows:

strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10


Two Dimensional array of character

A list of names can be treated as a table of strings and a two-dimensional character array can be used to store the entire list.For example, a charater array cities[3][5] used to store a list of 3 names, each of length not more than 5 characters.Let us take a table of three cities:

G o a \0
P u n e \0
U n a \0


This table can be declared as:

char city[][] { "Goa"; "Pune"; "Una"; }


To access the name of the ith city in the list, we write

city[i-1];


where, city[0] denotes "Goa",city[1] denotes "Pune",city[2] denotes "Una",