logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Implement String in STL

Posted on October 1, 2013 by Manish
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.
« Prev Page - C++ Program to Implement Binomial Heap
» Next Page - C++ Program to Implement List in STL

« C++ Program to Implement Fibonacci Heap
C++ Program to Implement List in STL »

Deep Dive @ Sanfoundry:

  1. C Programming Examples using Recursion
  2. C# Programming Examples on Sorting
  3. C Programming Examples on Strings
  4. C# Programming Examples on Gaming
  5. C# Programming Examples on Files
  6. C# Programming Examples on Interfaces
  7. C# Programming Examples on Exceptions
  8. C# Programming Examples on Functions
  9. C++ Programming Examples on Set & String Problems & Algorithms
  10. C Programming Examples on Set & String Problems & Algorithms
Manish Bhojasia
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 | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.