Comparing, Swapping with functions and relational operators of String :- In string class we can perform comparison and swapping between two strings. For it, in string swap( ) and compare( ) are used and to show relations between string, relational operator e.g. !=, ==, >, <, <=, >=, etc. are provided.
C++ program for swapping, comparing and relational operators of string.
#include<iostream.h>#include<conio.h>
#include<string.h>
void main(){
string s1("abcde"),s2("abcfg");
cout<<"Original strings are:-"<<endl<<"S1= "<<s1<<endl<<"S2= "<<s2<<endl;
getch();
cout<<"Swapping function..."<<endl;
s1.swap(s2);
cout<<"After swapping S1= "<<s1<<endl<<"S2= "<<s2<<endl;
getch();
int x=0;
cout<<"Compare function..."<<endl;
x=s1.compare(s2);
if (x==0) cout<<"Both Strings are same"<<endl;
else cout<<"Both strings are different"<<endl;
getch();
if(s1>s2) cout<<s1<<" is greater than "<<s2<<endl;
else cout<<s2<<" is greater than "<<s1<<endl;
getch();
if(s1!="abc")
cout<<strings are unequal"<<endl;
else cout<<"Strings are equal "<<endl;
getch();
}
No comments:
Post a Comment