fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5.  
  6. int isSubstring(string s1, string s2)
  7. {
  8. int M = s1.length();
  9. int N = s2.length();
  10.  
  11. /* A loop to slide pat[] one by one */
  12. for (int i = 0; i <= N - M; i++) {
  13. int j;
  14.  
  15. /* For current index i, check for
  16.   pattern match */
  17. for (j = 0; j < M; j++)
  18. if (s2[i + j] != s1[j])
  19. break;
  20.  
  21. if (j == M)
  22. return i;
  23. }
  24.  
  25. return -1;
  26. }
  27.  
  28. int main() {
  29.  
  30. string str="ahmed";
  31. string str1="e";
  32. int found = isSubstring(str1,str);
  33.  
  34. cout<<found;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5508KB
stdin
Standard input is empty
stdout
3