Advertisement
tuldok89

Search String

May 17th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. void printn(int n)
  9. {
  10.     cout << n << " ";
  11. }
  12.  
  13. int main()
  14. {
  15.     string string1, string2;
  16.     int pos = -1;
  17.     vector<int> lists;
  18.  
  19.     cout << "Enter a string: \n";
  20.     getline(cin, string1);
  21.  
  22.     cout << "\nEnter a string to find: \n";
  23.     getline(cin, string2);
  24.  
  25.     for(;;)
  26.     {
  27.         pos = string1.find(string2, pos+1);
  28.         if (pos != string::npos)
  29.             lists.push_back(pos+1);
  30.         else
  31.             break;
  32.     }
  33.  
  34.     if (!lists.empty())
  35.     {
  36.         cout << "\nString found at position/s: ";
  37.         for_each(lists.begin(), lists.end(), printn);
  38.     }
  39.     else
  40.     {
  41.         cout << "\nString not found.";
  42.     }
  43.  
  44.     cout << endl;
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement