String Handling Functions in C

C programming language provides a set of pre-defined functions called string handling functions to work with string values. The string handling functions are defined in a header file called string.h. Whenever we want to use any string handling function we must include the header file called string.h.

The following table provides most commonly used string handling function and their use…

FunctionSyntax (or) ExampleDescription
strcpy()strcpy(string1, string2)Copies string2 value into string1
strncpy()strncpy(string1, string2, 5)Copies first 5 characters string2 into string1
strlen()strlen(string1)returns total number of characters in string1
strcat()strcat(string1,string2)Appends string2 to string1
strncat()strncpy(string1, string2, 4)Appends first 4 characters of string2 to string1
strcmp()strcmp(string1, string2)Returns 0 if string1 and string2 are the same;
less than 0 if string1<string2; greater than 0 if string1>string2
strncmp()strncmp(string1, string2, 4)Compares first 4 characters of both string1 and string2
strcmpi()strcmpi(string1,string2)Compares two strings, string1 and string2 by ignoring case (upper or lower)
stricmp()stricmp(string1, string2)Compares two strings, string1 and string2 by ignoring case (similar to strcmpi())
strlwr()strlwr(string1)Converts all the characters of string1 to lower case.
strupr()strupr(string1)Converts all the characters of string1 to upper case.
strdup()string1 = strdup(string2)Duplicated value of string2 is assigned to string1
strchr()strchr(string1, ‘b’)Returns a pointer to the first occurrence of character ‘b’ in string1
strrchr()‘strrchr(string1, ‘b’)Returns a pointer to the last occurrence of character ‘b’ in string1
strstr()strstr(string1, string2)Returns a pointer to the first occurrence of string2 in string1
strset()strset(string1, ‘B’)Sets all the characters of string1 to given character ‘B’.
strnset()strnset(string1, ‘B’, 5)Sets first 5 characters of string1 to given character ‘B’.
strrev()strrev(string1)It reverses the value of string1
#include<stdio.h>
#include<string.h> 
int main()
{
char name[50],name1[50],name2[50],ch='B';
char *x;      //pointer
printf("Enter 1st string: ");
gets(name);
printf("Enter 2nd string:  ");
gets(name1);//input second string

printf("strlen() function: %d\n",strlen(name));

printf("strcat() function: %s\n",strcat(name,name1));

strcpy(name2,name);
printf("strcpy() function: %s\n",name2);

printf("strcmp() function: %d\n",strcmp(name,name1));

printf("strlwr() function: %s\n",strlwr(name));

printf("strupr() function: %s\n",strupr(name));

x=strchr(name,ch);
printf("strchr()function. The string after ch is: %s\n",x);

x=strstr(name,name1);
printf("strstr() function: %s",x);
return 0;
}
Output:-
Enter 1st string:  The bell rings
Enter 2nd string:  bell
strlen() function: 14
strcat() function: The bell ringsbell
strcpy() function: The bell ringsbell
strcmp() function: -1
strlwr() function: the bell ringsbell
strupr() function: THE BELL RINGSBELL
strchr()function. The string after ch is: BELL RINGSBELL
strstr() function: 

Other important library functions are:

  • strncmp(str1, str2, n) :it returns 0 if the first n characters of str1 is equal to the first n characters of str2, less than 0 if str1 < str2, and greater than 0 if str1 > str2.
  • strncpy(str1, str2, n) This function is used to copy a string from another string. Copies the first n characters of str2 to str1
  • strchr(str1, c): it returns a pointer to the first occurrence of char c in str1, or NULL if character not found.
  • strrchr(str1, c): it searches str1 in reverse and returns a pointer to the position of char c in str1, or NULL if character not found.
  • strstr(str1, str2): it returns a pointer to the first occurrence of str2 in str1, or NULL if str2 not found.
  • strncat(str1, str2, n) Appends (concatenates) first n characters of str2 to the end of str1 and returns a pointer to str1.
  • strlwr() :to convert string to lower case
  • strupr() :to convert string to upper case
  • strrev() : to reverse string

Converting a String to a Number

In C programming, we can convert a string of numeric characters to a numeric value to prevent a run-time error. The stdio.h library contains the following functions for converting a string to a number:

  • int atoi(str) Stands for ASCII to integer; it converts str to the equivalent int value. 0 is returned if the first character is not a number or no numbers are encountered.
  • double atof(str) Stands for ASCII to float, it converts str to the equivalent double value. 0.0 is returned if the first character is not a number or no numbers are encountered.
  • long int atol(str) Stands for ASCII to long int, Converts str to the equivalent long integer value. 0 is returned if the first character is not a number or no numbers are encountered.

The following program demonstrates atoi() function:

#include <stdio.h>
int main()
{char *string_id[10];
  int ID;
  printf("Enter a number: ");
  gets(string_id);
  ID = atoi(string_id);
   printf("you enter %d  ",ID);
  return 0;}

Output:

Enter a number: 221348
you enter 221348
  • A string pointer declaration such as char *string = “language” is a constant and cannot be modified.
Follow Us On