Home »
C solved programs
string.h header file library functions example programs.
This section contains example programs on string.h header file. This header file contains all kind of string related function for string manipulation. Some of the functions are : strlen(), strcpy(), strupr(), strlwr(), strrev(), strcmp(), strcmpi(), strcat(), strncpy(), memset().
List of string.h header file’s library functions with explanation and example programs
1) strlen() - String Length
strlen function returns length of the string without counting null character.
/*C example of strlen().*/
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="www.includehelp.com";
int length;
//string length
length=strlen(str);
printf("String Length: %d\n",length);
return 0;
}
String Length: 19
2) strupr() - String Upper
strupr converts string into uppercase.
3) strlwr() - String Lower
strlwr converts string into lowercase.
/*C example of strupr() and strlwr().*/
#include<stdio.h>
#include<string.h>
int main()
{
char str[]="Hello Wolrd.";
printf("Value of str: %s\n",str);
//convert into uppercase
strupr(str);
printf("Value of str (Uppercase): %s\n",str);
//convert into lowercase
strupr(str);
printf("Value of str (Lowercase): %s\n",str);
return 0;
}
Value of str: Hello World.
Value of str (Uppercase): HELLO WORLD.
Value of str (Lowercase): hello world.
4) strrev() - String Reverse
strrev reverses the string.
/* C example program of strrev().*/
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter string: ");
gets(str);
printf("Entered string is: %s\n",str);
//Reverse string
strrev(str);
printf("Reversed string is: %s\n",str);
return 0;
}
Enter string: Hello World
Entered string is: Hello World
Reversed string is: dlroW olleH
5) strcpy() - String Copy
strcpy copies one string to another string, in this function there will be two parameters second parameter’s values will be copied into first parameter’s variable.
/* C example program of strcpy().*/
#include<stdio.h>
#include<string.h>
int main()
{
char str1[30];
char str2[30];
printf("Enter string 1: ");
gets(str1);
//copy str1 into str2
strcpy(str2,str1);
printf("str1: %s \nstr2: %s \n",str1,str2);
return 0;
}
Enter string 1: Hello World.
str1: Hello World.
str2: Hello World.
6) strcmp() - String Compare
strcmp compares two strings and return 0, less than 0 and greater than 0 based on strings, if strings are same function will return 0, other function will return difference of first dissimilar character, difference may be positive or negative.
7) strcmpi() - String Comparing Ignoring case
strcmpi compares two strings ignoring case sensitivity and return 0, less than 0 and greater than 0 based on strings, if strings are same function will return 0, other function will return difference of first dissimilar character, difference may be positive or negative.
/* C example program of strcmp() and strcmpi().*/
#include<stdio.h>
#include<string.h>
int main()
{
char str1[30];
char str2[30];
printf("Enter string1: "); gets(str1);
printf("Enter string2: "); gets(str2);
//using strcmp
printf("Using strcmp:\n");
if(strcmp(str1,str2)==0)
printf("strings are same.\n");
else
printf("strings are not same.\n");
//using strcmp
printf("Using strcmpi:\n");
if(strcmpi(str1,str2)==0)
printf("strings are same.\n");
else
printf("strings are not same.\n");
return 0;
}
Enter string1: Hello World
Enter string2: hello world
Using strcmp:
strings are not same.
Using strcmpi:
strings are same.
8) strcat() - String Concatenate
strcat concatenates second string with first string. This function will take two parameters after executing this function, value of second parameter will be concatenated with first string.
/* C example program of strcat() .*/
#include<stdio.h>
#include<string.h>
int main()
{
char title[5],fName[30],lName[30];
char name[100]={0}; //assign null
printf("Enter title (Mr./Mrs.): "); gets(title);
printf("Enter first name: "); gets(fName);
printf("Enter last name: "); gets(lName);
//create complete name using string concatenate
strcat(name,title);
strcat(name," ");
strcat(name,fName);
strcat(name," ");
strcat(name,lName);
strcat(name," ");
printf("Hi.... %s\n",name);
return 0;
}
Enter title (Mr./Mrs.): Mr.
Enter first name: Mike
Enter last name: Thomas
Hi.... Mr. Mike Thomas
9) strncpy() - String Copy with finite number of characters
strncpy copies first N characters. This function will copy first N characters from second parameter’s value (variable) in first parameter’s variable.
/*C example of strncpy().*/
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30]={0};
char str2[30]={0};
printf("Enter string1: "); gets(str1);
//copy first 3 characters of str1
//before copy insert null in target string
strncpy(str2,str1,3);
printf("After copying str2 is: %s\n",str2);
return 0;
}
Enter string1: Hello
After copying str2 is: Hel
10) memset() - Memory Set
memset function is used to set (assign) given number of characters in string. there will be three parameters first parameter is targeted string variable, second parameter is the character (any one byte value) that will be assigned and third one is number of blocks in which given value will be assigned.
/*C example of strncpy().*/
#include <stdio.h>
#include <string.h>
int main()
{
char str[30];
//set NULL value to complete string
memset(str,'\0',30);
printf("str: %s\n",str);
/* set SPACE to first 29 characters
do not assign space or other character
to last character, it contains NULL.
*/
memset(str,' ',29);
printf("str: %s\n",str);
//set A to first 10 characters and B to next 5 characters
memset(str,'A',10);
memset(str+10,'B',5);
printf("str: %s\n",str);
return 0;
}
str:
str:
str: AAAAAAAAAABBBBB