Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream> //pgm to implement string functions
- #include<string>
- using namespace std;
- int main()
- {
- string str1, str2, str3;
- char ch, s1[30],s2[30];
- int opt,pos,spos,num,found;
- cout<<"\nEnter the main string : \n";
- gets(s1);
- str1=s1;
- cout<<"\nEnter the second string : \n";
- gets(s2);
- str2=s2;
- do
- {
- cout<<"\nEnter option : \n1 - Compare String\n2 - Append String\n3 - Insert String\n4 - Find String\n";
- cout<<"5 - Erase String\n6 - Check if substring\n7 - Replace String\n8 - Assign String\n";
- cout<<"9 - + String\n10 - = String\n11 - == String\n12 - > String\n13 - < String\n\n";
- cin>>opt;
- switch(opt)
- {
- case 1:
- if (str1.compare(str2) != 0)
- cout <<"\nThe strings are not the same";
- else
- cout <<"\nBoth the strings are same";
- break;
- case 2:
- str1.append(str2);
- cout<<str1;
- break;
- case 3:
- cout<<"\Enter the position for inserting the string : ";
- cin>>pos;
- str1.insert(pos,str2);
- cout<<str1;
- break;
- case 4:
- found=str1.find(str2);
- if (found!=string::npos)
- cout <<str2<<" found at: " <<int(found)<<endl;
- else
- cout<<"\nThe string is not found ! ";
- break;
- case 5:
- cout<<"\nEnter the starting position to be erased : ";
- cin>>spos;
- cout<<"\nEnter the number of letters to be erased : ";
- cin>>num;
- str1.erase (spos,num);
- cout<<"\n"<<str1<<endl;
- break;
- case 6:
- cout<<"\nEnter the starting position of the required substring : ";
- cin>>spos;
- cout<<"\nEnter the number of letters needed : ";
- cin>>num;
- str2 = str1.substr (spos,num);
- cout<<"\n"<<str2<<endl;
- break;
- case 7:
- cout<<"\nEnter the starting position from which the string is to be replaced : ";
- cin>>pos;
- cout<<"\nEnter the number of spaces to be replaced : ";
- cin>>spos;
- str1.replace(pos,spos,str2);
- cout<<"\n"<<str1;
- break;
- case 8:
- cout<<"\nEnter the starting position from which the string is to be assigned : ";
- cin>>pos;
- cout<<"\nEnter the number of spaces to be assigned : ";
- cin>>spos;
- str2.assign(str1,pos,spos);
- cout<<"\n"<<str1;
- break;
- case 9:
- str3=str1+str2;
- cout<<"\n"<<str3;
- break;
- case 10:
- str1=str2;
- cout<<"\n"<<str1<<endl;
- break;
- case 11:
- if(str1==str2)
- cout<<"\nBoth the strings are equal";
- else
- cout<<"\nBoth the strings are not equal";
- break;
- case 12:
- if(str1>str2)
- {
- cout<<"\nThe first string is greater than the second!\n";
- cout<<str1<<" is greater than "<<str2<<"\n";
- }
- else if(str2>str1)
- {
- cout<<"\nThe second string is greater than the first!\n";
- cout<<str2<<" is greater than "<<str1<<"\n";
- }
- else
- cout<<"\nEqual";
- break;
- case 13:
- if(str1<str2)
- {
- cout<<"\nThe first string is smaller than the second!\n";
- cout<<str1<<" is smaller than "<<str2<<"\n";
- }
- else if(str2<str1)
- {
- cout<<"\nThe second string is greater than the first!\n";
- cout<<str2<<" is smaller than "<<str1<<"\n";
- }
- else
- cout<<"\nEqual";
- break;
- default:
- cout<<"\nInvalid option";
- }
- cout<<"\nDo you want to continue? [y/n] : ";
- cin>>ch;
- }while(ch=='y'||ch=='Y');
- }
Add Comment
Please, Sign In to add comment