fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. void func(char *s)
  4. {
  5. int n = strlen(s);
  6. printf("input = %s\n", s);
  7. for (int i = 1; i <= n / 2; ++i)
  8. {
  9. if (n % i == 0)
  10. {
  11. if (strncmp(s, s + i, strlen(s + i)) == 0)
  12. {
  13. printf("repeat = %.*s\n\n", i, s);
  14. return;
  15. }
  16. }
  17. }
  18. printf("repeat unit not found!\n\n");
  19. return;
  20. }
  21. int main()
  22. {
  23. func("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  24. func("123412312341231234123123412312341231234123");
  25. func("oxoxoxoxoxoxoxoxxoxoxoxoxoxoxoxoxx");
  26. func("axaxa"); // repeat = ax
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
input  = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
repeat = a

input  = 123412312341231234123123412312341231234123
repeat = 1234123

input  = oxoxoxoxoxoxoxoxxoxoxoxoxoxoxoxoxx
repeat = oxoxoxoxoxoxoxoxx

input  = axaxa
repeat unit not found!