fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int SEARCH(char *TEXT, char *PATTERN)
  5. {
  6. int i,j;
  7. int M=strlen(PATTERN);
  8. int N=strlen(TEXT);
  9. for (i = 0; i <= N - M; i++)
  10. {
  11. for (j = 0; j < M; j++)
  12. {
  13. if (TEXT[i + j] != PATTERN[j])
  14. break;
  15. }
  16.  
  17. if (j == M)
  18. return i;
  19. }
  20. return -1;
  21. }
  22.  
  23. int main()
  24. {
  25. char TEXT[100],PATTERN[100];
  26. scanf("%s%s",TEXT,PATTERN);
  27.  
  28. int LOC=SEARCH(TEXT,PATTERN);
  29.  
  30. if(LOC==-1)
  31. cout<<"PATTERN is not in the TEXT" <<endl;
  32. else
  33. cout<<"PATTERN "<<PATTERN<<" is in the location "<<LOC+1<<endl;
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5284KB
stdin
AABABCA
AB
stdout
PATTERN AB is in the location 2