Friday 24 November 2017

C++ Program to Implement String in STL


Code:

#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--> "<
    while (1)
    {
        cout<<"\n---------------------"<
        cout<<"String Implementation in Stl"<
        cout<<"\n---------------------"<
        cout<<"1.Insert Substring in a String"<
        cout<<"2.Erase Substring from a String"<
cout<<"3.Append Substring to a String"<
        cout<<"4.Replace the String with a Substrng"<
        cout<<"5.Size of the String"<
        cout<<"6.Find substring in a String"<
        cout<<"7.Display the String"<
        cout<<"8.Exit"<
        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"<
            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: "<
    break;
        case 6:
            cout<<"Enter substring to be found: ";
            cin>>s;
            found = str.find(s);
            if (found != string::npos)
                cout <<"Substring "<
            else
                cout <<"Substring "<
            break;
        case 7:
            for (it = str.begin(); it != str.end(); ++it)
                cout<<*it;
            cout<
            break;
        case 8:
            exit(1);
            break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

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


More C++ Programs:













100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...