String Handling Functions in C

Following are some of the useful string handling functions supported by C.

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

These 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 program.
All these functions take either character pointer or character arrays as arguments.

  1. strlen()
    strlen() function returns the length of the string. strlen() function returns integer value.
    Example:
  2. strcpy()
    strcpy() function is used to copy one string to another. 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:

    Output:
    Learn C Online
  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:

    Output:
    Learn C Online
  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:

    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:

    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:

    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:

    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:

    Output:
    0

You may also like...

19 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. Manikanta says:

    Good

  15. Rajnik says:

    I like it

Leave a Reply

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