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.
/**
* C++ Program to Implement String in Stl
*/
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
int choice, pos, len;
string::iterator it;
size_t found;
string s, str = "This is a Test String.";
cout<<"Initial String is--> "<<str<<endl;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"String Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Substring in a String"<<endl;
cout<<"2.Erase Substring from a String"<<endl;
cout<<"3.Append Substring to a String"<<endl;
cout<<"4.Replace the String with a Substrng"<<endl;
cout<<"5.Size of the String"<<endl;
cout<<"6.Find substring in a String"<<endl;
cout<<"7.Display the String"<<endl;
cout<<"8.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the substring to be inserted: ";
cin>>s;
cout<<"Position after which substring to be inserted: ";
cin>>pos;
if (pos <= str.length())
str.insert(pos, s);
else
cout<<"Position out of bounds"<<endl;
break;
case 2:
cout<<"Position after which substring to be erased: ";
cin>>pos;
cout<<"Length of the substring to be deleted: ";
cin>>len;
str.erase(pos, len);
break;
case 3:
s = " This is an appended string.";
str.append(s);
break;
case 4:
s = "n example";
str.replace(9, 5, s);
break;
case 5:
cout<<"Size of the string: "<<str.size()<<endl;
break;
case 6:
cout<<"Enter substring to be found: ";
cin>>s;
found = str.find(s);
if (found != string::npos)
cout <<"Substring "<<s<<" found at " << found <<endl;
else
cout <<"Substring "<<s<<" not found"<<endl;
break;
case 7:
for (it = str.begin(); it != str.end(); ++it)
cout<<*it;
cout<<endl;
break;
case 8:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
$ 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.
Related Posts:
- Practice Programming MCQs
- Practice Computer Science MCQs
- Check Data Structure Books
- Practice Design & Analysis of Algorithms MCQ
- Check Computer Science Books