fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. // Function to check if str2 is a rotation of str1
  5. int isRotation(char *str1, char *str2) {
  6. // Check if lengths are the same
  7. if (strlen(str1) != strlen(str2)) {
  8. return 0; // NO
  9. }
  10.  
  11. // Create a new string by concatenating str2 with itself
  12. char temp[2 * strlen(str2) + 1];
  13. strcpy(temp, str2);
  14. strcat(temp, str2);
  15.  
  16. // Check if str1 is a substring of temp
  17. if (strstr(temp, str1) != NULL) {
  18. return 1; // YES
  19. }
  20.  
  21. return 0; // NO
  22. }
  23.  
  24. int main() {
  25. int T;
  26. scanf("%d", &T); // Read the number of test cases
  27.  
  28. // Process each test case
  29. for (int t = 0; t < T; t++) {
  30. char str1[101], str2[101]; // Maximum length of strings (based on constraints)
  31. scanf("%s %s", str1, str2);
  32.  
  33. // Check if str2 is a rotation of str1
  34. if (isRotation(str1, str2)) {
  35. printf("YES\n");
  36. } else {
  37. printf("NO\n");
  38. }
  39. }
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 5288KB
stdin
2  // Test Cases
abacd  // testcase 1
acdab
coder  // testcase 2
cored
stdout
NO
NO