String handling in C++

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.
         For example, The word is Whereisstuff
         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

Example:

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);

Example program strlen():

#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();
}

Output:
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);

Example program strcpy():

#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();
}

Output:
3. The strcat() function:
  • It is used to concatenate or combine, two strings together and forms a new string.
Syntax:

strcat(string1,string2);

Example program strcat():

#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();
}

Output:
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);

Example program strcmp():

#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();
}

Output:
if condition:
else condition:
5. The strrev() function;
  • It is used to reverse a string.
Syntax:

strrev(string);

Example program strrev():

#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();
}

Output:
6. The strlwr() function:
  • It is used to convert a string into lower cases (small letters).
Syntax:

strlwr(string);

Example program strlwr():

#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();
}

Output:
7. The strupr() function:
  • It is used to convert a string into upper case(capital letters).
Syntax:

strupr(string);

Example program strupr():

#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();
}

Output:

8. The stricmp() function:
  • It is used to compare two strings, no difference between small and capital letters.
Syntax:

stricmp(string1,string2);

Example program stricmp():

#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();
}

Output:
if condition:
else condition:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me