String handling functions in C

In this article, you will understand different string handling functions in C. By the end of this article, you will learn how to write C programs to perform following string operations –  find length of a string, compare 2 strings, copy one string to another, concatenate 2 strings etc.

Following are some of the useful string handling functions in C.

  1. strlen()
  2. strcpy()
  3. strncpy()
  4. strcat()
  5. strncat()
  6. strcmp()
  7. strncmp()
  8. strcmpi()
  9. strncmpi()

These string handling functions are defined in string.h header file. Hence you need to include this header file whenever you use these string handling functions in your C program.

Following are the most commonly used string handling functions in C. All these functions take either character pointer or character arrays as arguments.

  1. strlen()
    strlen() is used to find length of a string. strlen() function returns the length of a string. It returns integer value.
    Example:

    char *str = "Learn C Online";
    int strLength;
    strLength = strlen(str);    //strLength contains the length of the string i.e. 14
    

    In the above example, I have declared a string named str and assigned a value – “Learn C Online” to it. In the next line, I have declared an int variable named strLength which will be used to store length of the string named str. The next line uses in-built function strlen() which accepts one parameter i.e., a string named str.
    strlen() function will return the length (int type) of the string which be stored in variable strLength.
    After execution of this line, variable strLength should hold integer value 14.

    Here is the program that demonstrate usage of strlen() string function – visit Find string length using in-built C string function

  2. strcpy()
    strcpy() function is used to copy one string to another. strcpy() function accepts 2 parameters. First parameter is the destination string i.e. the string variable used to copy the string into. Second parameter is the source string i.e. the string variable or string value that needs to be copied to destination string variable.

    The Destination_String should be a variable and Source_String can either be a string constant or a variable.

    Syntax:
    strcpy(Destination_String,Source_String);

    Example:

    char *Destination_String;
    char *Source_String = "C String functions";
    strcpy(Destination_String,Source_String);
    printf("%s", Destination_String);
    

    Output:
    C String functions

    In the above example, I have declared 2 strings. First string variable named “Destination_String” and second string named “Source_String” with a string value assigned to it.
    In the next line, I am using strcpy() function with 2 parameters. This in-built string function will copy the value of Source_String into Destination_String. The variable Destination_String will now hold the value – “C String functions”.

  3. strncpy()
    strncpy() is used to copy only the left most n characters from source to destination. The Destination_String should be a variable and Source_String can either be a string constant or a variable.
    Syntax:
    strncpy(Destination_String, Source_String,no_of_characters);
  4. strcat()
    strcat() is used to concatenate two strings.
    The Destination_String should be a variable and Source_String can either be a string constant or a variable.
    Syntax:
    strcat(Destination_String, Source_String);
    Example:

    char *Destination_String ="Learn ";
    char *Source_String = "String functions";
    strcat(Destination_String, Source_String);
    puts( Destination_String);

    Output:
    Learn String functions

  5. strncat()
    strncat() is used to concatenate only the leftmost n characters from source with the destination string.
    The Destination_String should be a variable and Source_String can either be a string constant or a variable.
    Syntax:
    strncat(Destination_String, Source_String,no_of_characters);
    Example:

    char *Destination_String="Visit ";
    char *Source_String = "Learn C Online is a great site";
    strncat(Destination_String, Source_String,14);
    puts( Destination_String);

    Output:
    Visit Learn C Online

  6. strcmp()
    strcmp() function is use two compare two strings. strcmp() function does a case sensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
    Syntax:
    int strcmp(string1, string2);
    This function returns integer value after comparison.
    Value returned is 0 if two strings are equal.
    If the first string is alphabetically greater than the second string then, it returns a positive value.
    If the first string is alphabetically less than the second string then, it returns a negative value
    Example:

    char *string1 = "C Programming";
    char *string2 = "C Programming";
    int ret;
    ret=strcmp(string1, string2);
    printf("%d",ret);
    

    Output:
    0

  7. strncmp()
    strncmp() is used to compare only left most ‘n’ characters from the strings.
    Syntax:
    int strncmp(string1, string2,no_of_chars);
    This function returns integer value after comparison.
    Value returned is 0 if left most ‘n’ characters of two strings are equal.
    If the left most ‘n’ characters of first string is alphabetically greater than the left most ‘n’ characters of second string then, it returns a positive value.
    If the left most ‘n’ characters of first string is alphabetically less than the left most ‘n’ characters of second string then, it returns a negative value
    Example:

    char *string1 = "Learn C Online is a great site";
    char *string2 = "Learn C Online";
    int ret;
    ret=strncmp(string1, string2,7);
    printf("%d",ret);
    

    Output:
    0

  8. strcmpi()
    strcmpi() function is use two compare two strings. strcmp() function does a case insensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
    Syntax:
    int strcmpi(string1, string2);
    This function returns integer value after comparison.
    Example:

    char *string1 = “Learn C Online”;
    char *string2 = “LEARN C ONLINE”;
    int ret;
    ret=strcmpi(string1, string2);
    printf("%d",ret);

    Output:
    0

  9. strncmpi()
    strncmpi() is used to compare only left most ‘n’ characters from the strings. strncmpi() function does a case insensitive comparison.
    Syntax:
    int strncmpi(string1, string2,no_of_chars);
    This function returns integer value after comparison.
    Example:

    char *string1 = "Learn C Online is a great site";
    char *string2 = "LEARN C ONLINE";
    int ret;
    ret=strncmpi(string1, string2,7);
    printf("%d",ret);
    

    Output:
    0

You may also like...

22 Responses

  1. sushma says:

    great

  2. chunli99 says:

    It would be nice of you to include other examples of the output being different from the value of zero. But lovely work none the less. <3

  3. Anonymous says:

    Good demonstration but u must also include strstr() function; for finding first occurrence of a string in other string.

  4. Unknown says:

    just want to confirm if a blank space is included as a one character or not??
    i.e “learn c online” takes 14 characters or 12 ??

  5. LearnCOnline says:

    Yes… blank space is considered as a character

  6. i want to whether in strcpy destination string must be compulsarily a variable or not

  7. i want to know that on which criteria does the 2 strings are compared?

  8. Anonymous says:

    what function should be use to arrange 5 names in alphabetical order using string?

  9. Anuj Rana says:

    what is difference between strcmp() and strcmpi()….

  10. Christopher says:

    @Anuj: strcmp compares strings and case matters.
    strcmpi compares strings, but the case doesn’t matter.

  11. good !!! very easy to understand

  12. Anonymous says:

    have to give other functions also like strstr ,strrchr,strchr etc..

  13. mani khan says:

    thnx for very good details

  14. Rajnik says:

    I like it

  15. Ruheena tabassum says:

    Good explanation. Write the syntax in brief.

Leave a Reply

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