String Handling


What are strings?

•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 as strings.

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‟).

•E. g. :  char name[]={„A‟, „B‟, „C‟, „D‟, „E‟,‟\0‟};

Each character in an array occupies one byte of memory and the last character is always „\0‟.

•This „\0‟ is just a one character, with the \indicating that what follows is something special.

•The null character „\0‟ is automatically inserted in C. Hence, the programmer doesn‟t need to add it up in the string.

•The elements of the character array are stored in contiguous memory locations.


Program to demonstrate printing of a string


char name[]=“abcd”;

inti= 0;

while (name[i]!=„\0‟)


printf(“%c”, name[i]);




——–This will print each character one by one ———


char name[]=“abcd”;

printf(“%s”, name); //—-the %s used is a format specification for printing out a string.



The same specification can be used to receive a string from the keyboard.

char name[25];

printf(“Enter your name”);

scanf(“%s”, name); // ——(%c.. Till \0 encountered)

printf(“hello %s”, name);


Points to Remember

•The length of the string should not exceed the dimension of the character array.

▫This is because C compiler doesn‟t perform bound checking on character arrays.

▫Hence, if we carelessly exceed the bounds there is danger of over-writing some important information.

•scanf() is not capable of receiving multi-word strings. Therefore, fields having multiple words in its are unacceptable.

▫The way to get around this limitation is by using the function gets().


Use of gets() and puts() functions

char name[25];

printf(“\n Enter your full name”);




—-puts () function ca display only one string at a time.

Also, puts() unlike printf() places the cursor on the next line. So no need to add \n in the code.

—-gets() receives one string at a time, but accepts multi-word string.


 String library function

Function Use
strlen Finds length of the string
strlwr Convertsa string to lower case
strupr Converts a string toupper case
strcat Appendsone string at the end of another
Strncat Appends first „n‟ characters ofa string at the end of another
strcpy Copiesa string into another
Strncpy Copies first „n‟ characters of string into another
Strcmp Compares to strings
Strncmp Compares first „n‟ characters of two strings
Strcmpi Compares two strings without regard to case(“i” denotes that this function ignores case)
Stricmp Compares two strings with regard to case (similar to strcmpi)
Strdup Duplicates a string
Strchr Finds first occurrence of a given character in a string
Strrchar Finds last occurrence of a given character in a string.
Strstr Finds first occurrence of a given string in another
Strset Set all character of a string to a given character
Strnset Set first character of a string to a given character
Strrev Reverse the string









A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)