String manipulation:
- In the ‘C++’ language, the group of characters, digits, and symbols enclosed within quotation marks are called as a string, otherwise, strings are an array of characters.
- The string is nothing but, it is a collection of characters or words.
Here, w, h, e, r, e, i, s, t, u, f, f such as characters and Whereisstuff as a string
- The null character (‘\0’) is used to mark the end of the string.
Syntax:
char name[]=”String”; //initialization
or
char name[Set maximum length of the string];//declaration
char name[]=”String”; //initialization
or
char name[Set maximum length of the string];//declaration
Example:
char name[]=”whereisstuff”;
or
char name[20];
char name[]=”whereisstuff”;
or
char name[20];
String handling function:
Most widely used functions are,
1. The strlen() function:
- It is used to count and return the number of characters presents.
Syntax:
Variable_name=strlen(string);
Variable_name=strlen(string);
Example program strlen():
Output:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
char b[]="whereisstuff";
int len=strlen(a);
clrscr();
cout<<"\n Enter a string: ";cin>>a;
cout<<"\n The length of "<<a<<" string is: "<<len;
cout<<"\n String length of "<<b<<" is : "<<strlen(b);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
char b[]="whereisstuff";
int len=strlen(a);
clrscr();
cout<<"\n Enter a string: ";
cin>>a;
cout<<"\n The length of "<<a<<" string is: "<<len;
cout<<"\n String length of "<<b<<" is : "<<strlen(b);
getch();
}
2. The strcpy() function:
- It is used to copy the content of one string to another and it almost works like a string assignment operator.
Syntax:
strcpy(string1,string2);
strcpy(string1,string2);
Example program strcpy():
Output:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30];
char str2[]="whereisstuff";
clrscr();
strcpy(str1,str2);
cout<<"\n Original string is "<<str2;
cout<<"\n Copied string is "<<str1;
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30];
char str2[]="whereisstuff";
clrscr();
strcpy(str1,str2);
cout<<"\n Original string is "<<str2;
cout<<"\n Copied string is "<<str1;
getch();
}
3. The strcat() function:
- It is used to concatenate or combine, two strings together and forms a new string.
Syntax:
strcat(string1,string2);
strcat(string1,string2);
Example program strcat():
Output:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[]="whereisstuff";
char str2[]=".com";
clrscr(); cout<<"\n String1 is : "<< str1;cout<<"\n String2 is : "<< str2;
cout<<"\n Concatenate string is "<< strcat(str1,str2);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[]="whereisstuff";
char str2[]=".com";
clrscr();
cout<<"\n String1 is : "<< str1;
cout<<"\n String2 is : "<< str2;
cout<<"\n Concatenate string is "<< strcat(str1,str2);
getch();
}
4. The strcmp() function:
- It is used to compare two strings to find out whether they are the same or different.
Syntax:
strcmp(string1,string2);
strcmp(string1,string2);
Example program strcmp():
Output:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
clrscr();
cout<<"\n Enter two string: ";
cin>>str1>>str2;
if (strcmp(str1,str2)==0)
{
cout<<"\n Compared string is right because
two strings are the same case.";
}
else
{
cout<<"\n Compared string is wrong because
two strings are different case";
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
clrscr();
cout<<"\n Enter two string: ";
cin>>str1>>str2;
if (strcmp(str1,str2)==0)
{
cout<<"\n Compared string is right because
two strings are the same case.";
}
else
{
cout<<"\n Compared string is wrong because
two strings are different case";
}
getch();
}
if condition:
else condition:
5. The strrev() function;
- It is used to reverse a string.
Syntax:
strrev(string);
strrev(string);
Example program strrev():
Output:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20];
clrscr();
cout<<"\n Enter a string: ";
cin>>str1;
cout<<"\n The reversed string is "<<strrev(str1);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20];
clrscr();
cout<<"\n Enter a string: ";
cin>>str1;
cout<<"\n The reversed string is "<<strrev(str1);
getch();
}
6. The strlwr() function:
- It is used to convert a string into lower cases (small letters).
Syntax:
strlwr(string);
strlwr(string);
Example program strlwr():
Output:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20];
clrscr();
cout<<"\n Enter a string in capital letters: ";
cin>>str1;
cout<<"\n Lower string is "<< strlwr(str1);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20];
clrscr();
cout<<"\n Enter a string in capital letters: ";
cin>>str1;
cout<<"\n Lower string is "<< strlwr(str1);
getch();
}
7. The strupr() function:
- It is used to convert a string into upper case(capital letters).
Syntax:
strupr(string);
strupr(string);
Example program strupr():
Output:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20];
clrscr();
cout<<"\n Enter a string in small letters ";
cin>>str1;
cout<<"\n Upper string is "<<strupr(str1);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20];
clrscr();
cout<<"\n Enter a string in small letters ";
cin>>str1;
cout<<"\n Upper string is "<<strupr(str1);
getch();
}
8. The stricmp() function:
- It is used to compare two strings, no difference between small and capital letters.
Syntax:
stricmp(string1,string2);
stricmp(string1,string2);
Example program stricmp():
Output:
if condition:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
clrscr();
cout<<"\n Enter two string: ";
cin>>str1,>>str2;
if (stricmp(str1,str2)==0)
{
cout<<"\n Compared string is right because
two strings are the same.";
}
else
{
cout<<"\n Compared string is wrong because
two strings are different.";
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
clrscr();
cout<<"\n Enter two string: ";
cin>>str1,>>str2;
if (stricmp(str1,str2)==0)
{
cout<<"\n Compared string is right because
two strings are the same.";
}
else
{
cout<<"\n Compared string is wrong because
two strings are different.";
}
getch();
}
else condition:
No comments:
Post a Comment