C++ Program to Implement String in STL

«
»
This C++ Program demonstrates implementation of String in STL.

Here is source code of the C++ Program to demonstrate String in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /**
  2.  * C++ Program to Implement String in Stl
  3.  */
  4. #include <iostream>
  5. #include <string>
  6. #include <cstdlib>
  7. using namespace std;
  8. int main()
  9. {
  10.     int choice, pos, len;
  11.     string::iterator it;
  12.     size_t found;
  13.     string s, str = "This is a Test String.";
  14.     cout<<"Initial String is--> "<<str<<endl;
  15.     while (1)
  16.     {
  17.         cout<<"\n---------------------"<<endl;
  18.         cout<<"String Implementation in Stl"<<endl;
  19.         cout<<"\n---------------------"<<endl;
  20.         cout<<"1.Insert Substring in a String"<<endl;
  21.         cout<<"2.Erase Substring from a String"<<endl;
  22. 	cout<<"3.Append Substring to a String"<<endl;
  23.         cout<<"4.Replace the String with a Substrng"<<endl;
  24.         cout<<"5.Size of the String"<<endl;
  25.         cout<<"6.Find substring in a String"<<endl;
  26.         cout<<"7.Display the String"<<endl;
  27.         cout<<"8.Exit"<<endl;
  28.         cout<<"Enter your Choice: ";
  29.         cin>>choice;
  30.         switch(choice)
  31.         {
  32.         case 1:
  33.             cout<<"Enter the substring to be inserted: ";
  34.             cin>>s;
  35.             cout<<"Position after which substring to be inserted: ";
  36.             cin>>pos;
  37.             if (pos <= str.length())
  38.                 str.insert(pos, s);
  39.             else
  40.                 cout<<"Position out of bounds"<<endl;
  41.             break;
  42.         case 2:
  43.             cout<<"Position after which substring to be erased: ";
  44.             cin>>pos;
  45.             cout<<"Length of the substring to be deleted: ";
  46.             cin>>len;
  47.             str.erase(pos, len);
  48.             break;
  49.         case 3:
  50.             s = " This is an appended string.";
  51.             str.append(s);
  52.             break;
  53.         case 4:
  54.             s = "n example";
  55.             str.replace(9, 5, s);
  56.             break;
  57.         case 5:
  58.             cout<<"Size of the string: "<<str.size()<<endl;
  59. 	    break;
  60.         case 6:
  61.             cout<<"Enter substring to be found: ";
  62.             cin>>s;
  63.             found = str.find(s);
  64.             if (found != string::npos)
  65.                 cout <<"Substring "<<s<<" found at " << found <<endl;
  66.             else
  67.                 cout <<"Substring "<<s<<" not found"<<endl;
  68.             break;
  69.         case 7:
  70.             for (it = str.begin(); it != str.end(); ++it)
  71.                 cout<<*it;
  72.             cout<<endl;
  73.             break;
  74.         case 8:
  75.             exit(1);
  76.             break;
  77.         default:
  78.             cout<<"Wrong Choice"<<endl;
  79.         }
  80.     }
  81.     return 0;
  82. }

advertisement
$ g++ string.cpp
$ a.out
Initial String is--> This is a Test String.
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 1
Enter the substring to be inserted: example
Position after which substring to be inserted: 5
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 7
This exampleis a Test String.
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 2
Position after which substring to be erased: 5
Length of the substring to be deleted: 7
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 7
This is a Test String.
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 3
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 7
This is a Test String. This is an appended string.
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 4
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 7
This is an example String. This is an appended string.
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 5
Size of the string: 54
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 6
Enter substring to be found: string
Substring string found at 47
 
---------------------
String Implementation in Stl
 
---------------------
1.Insert Substring in a String
2.Erase Substring from a String
3.Append Substring to a String
4.Replace the String with a Substrng
5.Size of the String
6.Find substring in a String
7.Display the String
8.Exit
Enter your Choice: 8
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn