fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int rotate_match_sub(char *p1, char *p2, int n) {
  5. if (n == 0) return 0;
  6. if (strcmp(p1, p2) == 0) return 1;
  7. char c = p1[0];
  8.  
  9. unsigned int i;
  10. for (i = 0; i < strlen(p1) - 1; i++)
  11. p1[i] = p1[i + 1];
  12. p1[i] = c;
  13. return rotate_match_sub(p1, p2, n - 1);
  14. }
  15.  
  16. int rotate_match(char *p1, char *p2) {
  17. return rotate_match_sub(p1, p2, strlen(p1));
  18. }
  19.  
  20. #define N 100
  21. int main() {
  22. char s[N], p1[N], p2[N];
  23. strcpy(s, "abcde"); strcpy(p2, "deabc"); strcpy(p1, s);
  24. printf("%s:%s:%s\n", s, p2, rotate_match(p1, p2) == 1 ? "true" : "false");
  25.  
  26. strcpy(s, "abcde"); strcpy(p2, "cdeba"); strcpy(p1, s);
  27. printf("%s:%s:%s\n", s, p2, rotate_match(p1, p2) == 1 ? "true" : "false");
  28.  
  29. strcpy(s, "aaaaa"); strcpy(p2, "aaa"); strcpy(p1, s);
  30. printf("%s:%s:%s\n", s, p2, rotate_match(p1, p2) == 1 ? "true" : "false");
  31.  
  32. return 0;
  33. }
  34. /* end */
  35.  
Success #stdin #stdout 0s 4508KB
stdin
Standard input is empty
stdout
abcde:deabc:true
abcde:cdeba:false
aaaaa:aaa:false