3.2 String handling functions in c

 String-handling Functions: -

  • String-handling functions are provided in the <string.h> library.
  • These functions allow us to manipulate strings, such as copying, concatenating, comparing, finding lengths, and more.
  • Most frequently used string functions in c are:

                1. Strlen( )

                2. Strcpy( )

                3. Strcat( )

                4. Strcmp( )

                5. Strchr( )

                6. Strstr( )

 

1) strlen(str): -

  • The strlen( ) function is a string handling function which is used to find the length of a string.
  • It stands for “string length”.
  • It counts the number of characters in the string before the null character \0.
  • The null character \0 at the end of a string is not counted.

            Example: -

            Output: -


2) strcpy(dest, src): -

  • The strcpy( ) function is a string handling function which is used to copy the content of one string into another string.
  • It stands for “string copy”.
  • Here, Src – source string, dest – destination string.
  • The destination string will contain exactly the same characters as the source string after copying.
  • It also copies the null character \0 at the end.

            Example: -

            Output: -


3) strcat(dest, src): -

  • The strcat( ) function is a string handling function which is used to concatenate (append) one string to the end of another string.
  • It stands for “string concatenate”.
  • The destination string must have enough memory to hold both strings.
  • The source string is appended to the destination string.
  • The null terminator of destination is overwritten by the first character of source.
  • new null terminator is added at the end of the concatenated string.

            Example: -

            Output: -


4) strcmp(str1, str2): -

  • The strcmp( ) function is a string handling function which is used to compare two strings.
  • It stands for "string compare".
  • It compares strings lexicographically (character by character based on ASCII values) character by character.
  • It returns result as 0 if both strings are equal.
  • It returns result as Negative value if str1 is less than str2.
  • It returns result as Positive value if str1 is greater than str2.
  • Comparison is case-sensitive ('A' ≠ 'a').
  • Comparison stops at the first differing character or when null terminator is reached.

 

            Example: -

            Output: -

Explanation: -


5) strchr(str, ch): -

  • The strchr( ) function is a string handling function which is used to find the first occurrence of a character in a string.  
  • It stands for "string character".
  • Here str – string , ch – character.
  • It returns NULL if the character is not found.
  • The search includes the null terminator ('\0').
  • This function is case-sensitive.

            Example: -

            Output: -


6) strstr(str, substr): -

  • The strstr( ) function is a string handling function which is used to find the first occurrence of a substring within another string.
  • It stands for "string string" or "string in string".
  • It returns NULL, if the substring is not found.

            Example: -

            Output: -


Popular posts from this blog

operators in c programming

2.4 Arrays in c programming