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
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check Programming Books
- Check C++ Books
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for Computer Science Internship