Introduction to strings

A string is nothing but an one dimensional array of char. String literal are words surrounded by double quotation marks .

“Welcome to gyantoday”

In C every string is terminated by a null character ‘\0’  see an example below.

char str[20];

str is a 20 element character array, we can store up to 19 character in this array, because last character must be a null character.


Initializing array of type char

char str[10] = “gyantoday” ;In above char array str[9] is ‘\0’ null character, sizeof(str) is 10 and length of str is 9.Lets see one more example.

char str1[] = “gyan” /* Here sizeof(str1) is 5, but length of str1 is 4 */


There are number of library functions for string handling and manipulation. These all libraray functions defined in <string.h> header file. Some of the frequently used string handling library functions given below.

String handling Library functions Purpose of function
strlen() Find length of a string
strcpy() Copies a string into another
strncpy() Copies first n character of a string at the end of another string
strcmp() Compare two strings
strncmp() Compare first n character of two strings
strcat() Appends one string at the end of another string
strncat() Appends first n character of string at the end of another string
strset() Set all character of string to a given character
strrev() Reverse a string
strupr() Converts a string to upper case
strlwr() Converts a string to lower case
strcasecmp() Compare two strings case insensitive
strncasecmp() Compare first n character of two strings case insensitive


An example program of string handling.

#include<stdio.h>#include<string.h>int main(){

    char str[50];

    int length;

    printf(“Enter any string\n”);


    length = strlen(str);

    printf(“String length = %d\n”,length);

    printf(“Print given string\n”);


     return 0;


Suppose input string is “Welcome to gyantoday”  then output will be “Welcome to gyantoday”



Enter any string

Welcome to gyantoday

String length = 20

Print given string

Welcome to gyantoday


Write your  own program of strlen()

Write your  own program of strcpy()

Write your  own program of strncpy()

Write your  own program of strncmp()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.