C++ Program to Perform String Matching using Vectors

This C++ program implements string matching using vectors. 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.

  1. /*
  2.  * C++ Program to Implement String Matching Using Vectors
  3.  */
  4. #include<iostream>
  5. #include<string.h>
  6. #include<vector>
  7. using namespace std;
  8.  
  9. void input_string(vector<char>& str)
  10. {
  11.     char a;
  12.     while (1)
  13.     {
  14.         a = getchar();
  15.         if (a == '\n')
  16.         break;
  17.         str.push_back(a);
  18.     }
  19.     return;
  20. }
  21.  
  22. void print_string(vector<char> strn)
  23. {
  24.     for (std::vector<char>::iterator it = strn.begin();it != strn.end();++it)
  25.     {
  26.         cout<<*it;
  27.     }
  28.     return;
  29. }
  30.  
  31. int match_string(vector<char>& original, vector<char> match)
  32. {
  33.     vector<char>::iterator p,q, r;
  34.     int i = 0;
  35.  
  36.     p = original. begin();
  37.     while (r <= match.end() && p <= original.end())
  38.     {
  39.         r = match.begin();
  40.         while (*p != *r && p < original.end())
  41.         {
  42.             p++;
  43.             i++;
  44.         }
  45.  
  46.         q = p;
  47.         while (*p == *r && r <= match.end() && p<=original.end())
  48.         {
  49.             p++; i++;
  50.             r++;
  51.         }
  52.  
  53.         if (r >= match.end())
  54.         {
  55.             original.erase(original.begin(), q + 1);
  56.             return (i - match.size() + 1);
  57.         }
  58.  
  59.         if (p >= original.end())
  60.         return 0;
  61.         p = ++q;
  62.     }
  63. }
  64.  
  65.  
  66. int main()
  67. {
  68.     std::vector<char> original,match;
  69.     int i,result,k=0,sum=0;
  70.  
  71.     cout<<"Enter String:";
  72.     input_string(original);
  73.     cout<<"Enter Search Pattern:";
  74.     input_string(match);
  75.  
  76.     if (match.size() > original.size())
  77.     {
  78.         cout<<"Error:Original string too small.";
  79.     }
  80.  
  81.     do
  82.     {
  83.         result = match_string(original, match);
  84.         sum += result;    //to store previous found position
  85.         if (result > 0)
  86.         {
  87.             k++;
  88.             cout<<"\nMatch found from Position = "<<sum;
  89.         }
  90.      } while (result > 0);   //loop to find all patterns
  91.  
  92.      if (k == 0)
  93.          cout<<"Error:Match Not Found";
  94.      return 0;
  95. }

Output:
 
Enter String:all men went to apall mall
Enter Search Pattern:all
 
Match found from Position = 1
Match found from Position = 19
Match found from Position = 24

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.