This C++ program performs string matching using string library of C++. A text and a pattern is given as input. The pattern is searched for in the text and all instances of the pattern are given as output.
This C++ program is successfully compiled and tested on our system. The program output is given below.
/*
* C++ Program to Perform String Matching Using String Library
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string org, dup;
int result = -1, i = 1;
std::cout<<"Enter Original String:";
getline(std::cin, org);
std::cout<<"Enter Pattern String:";
getline(std::cin, dup);
do
{
result = org.find(dup, result + 1);
if (result != -1)
std::cout<<"\nInstance:"<<i<<"\tPosition:"<<result<<"\t";
i++;
} while (result >= 0);
return 0;
}
Output Enter Original String:All men went to the appall mall Enter Pattern String:all Instance:1 Position:23 Instance:2 Position:28
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Information Technology Internship
- Buy Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C++ Internship