String Functions:
There are many important string functions defined in "string.h" library. To use these functions, we need to include
<string.h> header file in our program.
Sr.No. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strrev(s1)
Returns reverse string.
6 strlwr(s1)
Returns string characters in lowercase.
7 strupr(s1)
Returns string characters in uppercase.
Programs on Strings:
Program to find length of a string without using string library function.
#include <stdio.h>
void main ()
Int len=0;
char a[20];
printf(“Enter a String”);
gets(a);
while(a[len]!==’\0’)
len++;
printf("The length of given string is %n",len);
getch();
}
Program to count the no of vowels and consonants in given string
#include<stdio.h>
#include<conio.h>
void main()
char str[80];
int vow=0,cons=0;
int c;
clrscr();
printf("Enter a string\n");
gets(str);
for(c=0;str[c]!='\0';c++)
if(str[c]=='a' || str[c]=='A' || str[c]=='e' ||str[c]=='E'||str[c]=='i' ||str[c]=='I' || str[c]=='o' ||str[c]=='O'|| str[c]=='u'
||str[c]=='U')
vow++;
else
cons++;
printf("\nvowels = %d \t Consonants= %d",vow,cons);
getch();
Program to check whether the string is palindrome or not
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
int i,j,n,flag=0;
char str[20];
clrscr();
printf("Enter String:");
gets(str);
n=strlen(str);
for(i=0,j=n-1;i<=n/2;i++,j--)
if(str[i]!=str[j])
flag=1;
break;
if(flag==0)
printf("String is palindrome");
else
printf("String is not Palindrome");
getch();
Program to copy the string from one character array to another character array
#include<stdio.h>
#include<conio.h>
void main()
char string1[80],string2[80];
int c;
clrscr();
printf("Enter a string\n");
gets(string1);
for(c=0;string1[c]!='\0';c++)
string2[c]=string1[c];
string2[c]='\0';
printf("\n%d characters copied in new string : %s",c,string2);
getch();
}
Program to check whether both the strings are equal or not
#include<stdio.h>
#include<conio.h>
void main()
char str1[30],str2[30];
int c=0;
clrscr();
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
while(str1[c]!='\0' && str2[c]!='\0' && (str1[c]==str2[c]))
c++;
if(str1[c]=='\0' && str2[c]=='\0')
printf("Strings are equal");
else
printf("Strings are not equal");
getch();
Program for concatenation of first, middle and last name
#include<stdio.h>
#include<conio.h>
void main()
char fname[30],mname[30],lname[30],name[100];
int c,cnt=0;
clrscr();
puts("Enter first name");
gets(fname);
puts("Enter middle name");
gets(mname);
puts("Enter last name");
gets(lname);
for(c=0;fname[c]!='\0';c++,cnt++)
name[cnt]=fname[c];
name[cnt++]=' ';
for(c=0;mname[c]!='\0';c++,cnt++)
name[cnt]=mname[c];
name[cnt++]=' ';
for(c=0;lname[c]!='\0';c++,cnt++)
name[cnt]=lname[c];
name[cnt]='\0';
puts(name);
getch();