fork(4) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int how_many_times(char* str1,char* str2)
  5. {
  6. int result=0,i,j=0;
  7. for(i=0;i<strlen(str2);i++)
  8. {
  9. if((str1[j]==str2[i])) // <<<=== Swapped i and j
  10. {
  11. while(str1[j]==str2[i]) // <<<=== Swapped i and j
  12. {
  13. j++;
  14. if(j==strlen(str1))
  15. {
  16. result++;
  17. j=0;
  18. }
  19. i++;
  20. }
  21. }
  22. else {
  23. i++;
  24. j = 0; // <<<=== Added
  25. }
  26. }
  27. return result;
  28. }
  29.  
  30. int main(void) {
  31. printf("%d\n", how_many_times("ab", "abab"));
  32. return 0;
  33. }
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
2