String functions
String functions are defined in
the string.h header file. These functions are used to manipulate the strings
as defined. The important functions of string.h are strcpy(), strcat(),
strlen(), strcmp(), strcmpi(), strev(), strupur() and strlwr(). The use of these functions are
given below:
strcpy()- to copy one string to another
strcat()- to concatenate two strings
into one
strlen()- to return length of string
strcmp()- to compare two strings for
equality
strcmpi()- to comaper two string without
case sensitivity
strev()- to return reverse of the
string
strupur()- to return uppercase string
for lowercase string
strlwr()- to return lowercase string
for uppercase string
To understand the use of above
functions, let consider the given example:
Example program to show the use of string functions
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20],i;
clrscr();
cout<<"Enter the string to find the
length:";
cin>>s1;
cout<<"\nLength of the string
is..."<<strlen(s1);
strcpy(s2,s1);
cout<<"\n\nCopied string
is..."<<s2;
cout<<"\n\nEnter 2 strings to be
concatenated:";
cin>>s1>>s2;
strcat(s1,s2);
cout<<"\nConcatenated string
is..."<<s1<<endl;
cout<<endl<<"\nEnter 2
strings to be compared:";
cin>>s1>>s2;
i=strcmp(s1,s2);
if(i==0)
cout<<"\nBoth strings are
equal\n";
else if(i<0)
cout<<s1<<" is less than
"<<s2<<endl;
else
cout<<s1<<" is greater than
"<<s2;
cout<<"\n\nEnter the string to
change into lower case:";
cin>>s1;
cout<<"\nLower case of the given
string is..."<<strlwr(s1);
cout<<"\n\nEnter the string to
change into upper case:";
cin>>s1;
cout<<"\nUpper case of the given
string is..."<<strupr(s1);
cout<<"\n\nEnter the string to be
reversed:";
cin>>s1;
cout<<"\nThe reversed string
is..."<<strrev(s1);
}
Output of Example program
Enter the string to find the length: welcome
Length of the string is...7
Copied string is...welcome;
Enter 2 strings to be concatenated: raj
kumar
Concatenated string is... rajkumar
Enter 2 strings to be compared: tata
bata
tata is greater than bata
Enter the string to change into lower case: WELCOME
Lower case of the given string is...welcome
Enter the string to change into upper case: computer
Upper case of the given string is...COMPUTER
Enter the string to be reversed: TAJ
The reversed string is...JAT
Example menu-driven program to show the use of string functions
#include<stdio.h>
#include<string.h>
main()
{
int a,b;
char s1[100],s2[100];
cout>>"Enter
a string.\n";
cin>>s1;
cout>>”Enter the operation you wish
to perform on the string:
\n1.
String Length\n2. String Reverse\n3. String Concatenation
\n4.
String Copy\n5. String Upper Case\n6. String Lower Case
\n7.
String Comparison\n";
cin>>a;
switch(a)
{
case 1:
{
b=strlen(s1);
cout<<"String Length =
”<<b;
break;
}
case 2:
{
cout<<"String Reverse:
”<<strrev(s1);
break;
}
case 3:
{
cout<<"Enter the target
string.\n";
cin>>s2;
strcat(s2,s1);
cout<<"Result - "<<s2;
break;
}
case 4:
{
strcpy(s2,s1);
cout<<"Copied String - "<<s2;
break;
}
case 5:
{
cout<<"The Upper Case of
String is "<<strupr(s1);
break;
}
case 6:
{
cout<<"The Lower Case of
String is "<<strlwr(s1);
break;
}
case 7:
{
cout<<"Enter the string
you wish to compare with the previous string.\n";
cin>>s2;
a=strcmp(s1,s2);
cout<<"String Compare
"<<a;
break;
}
default:
{
cout<<"You have not
entered a valid option.";
}
}
}
No comments:
Post a Comment