Jater

Count_string_in_string

Feb 18th, 2021 (edited)
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6.     int s_count = 0, f_str = -1, l_str = -1;
  7.     bool f = false;
  8.     string str, str_in;
  9.     cin >> str >> str_in;
  10.     for(int i=0; i<=str.size()-str_in.size(); i++){
  11.         if(str[i] == str_in[0]){
  12.             int in = 0;
  13.             for(int j=0; j<str_in.size(); j++){
  14.                 if(str[i+j] == str_in[j])
  15.                     in++;
  16.                 if(in == str_in.size()){
  17.                     s_count++;
  18.                     if(!f){
  19.                         f_str = i;
  20.                         f = !f;
  21.                     }
  22.                     if(f)
  23.                         l_str = i;
  24.                 }
  25.             }
  26.         }
  27.     }
  28.     cout<<s_count<<endl;
  29.     if(f_str > -1) cout<<f_str<<endl;
  30.     if(l_str > -1) cout<<l_str;
  31. }
Add Comment
Please, Sign In to add comment