Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4.  
  5. bool checkSub(string sub, string check, int check_start);
  6.  
  7. int main(void) {
  8.  
  9.     int count = 0;
  10.     string check = "hello my name is john";
  11.     string sub = "oh";
  12.  
  13.     for(int i = 0; i < check.length(); i++) {
  14.         if(check[i] == sub[0]) {
  15.             if(checkSub(sub, check, i))
  16.                 count++;
  17.         }
  18.     }
  19.  
  20.     std::cout << count << std::endl;
  21.  
  22.     return 0;
  23. }
  24.  
  25. bool checkSub(string sub, string check, int check_start) {
  26.     if(sub.length() > check.length()-check_start)
  27.         return false;
  28.  
  29.     for(int i = 0; i < sub.length(); i++) {
  30.         if(sub[i] != check[check_start+i])
  31.             return false;
  32.     }
  33.  
  34.     return true;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement