C Program to Rotate the given String

C Program to Rotate the given String: The idea is to run a loop from i = 0 to n – 1 ( n = length of the string) i.e for each point of rotation, copy the second part of the string in the temporary string and then copy the first part of the original string to the temporary string.

C Program to Rotate the given String

#include <stdio.h>
#include <string.h>
  void printChar(char *str, int n) {
        int i;
        for (i = n; i < strlen(str); i++) {
                printf("%c", *(str + i));
        }
        return;
  }
  void printRev(char *str, int n) {
        int i = 0;
        while (i <= n) {
                printf("%c", *(str + i));
                i++;
        }
        return;
  }
  int main() {
        char rotate[256];
        int i;
        printf("Enter input string to rotate:");
        fgets(rotate, 256, stdin);
        rotate[strlen(rotate) - 1] = '\0';
        printf("Rotating Input String:\n");
        if (strlen(rotate) <= 1) {
                printf("%s\n", rotate);
                return 0;
        }
        printf("%s\n", rotate);
        for (i = 1; i < strlen(rotate); i++) {
                printChar(rotate, i);
                printRev(rotate, i - 1);
                printf("\n");
        }
        return 0;
  }

Output of Program

Enter input string to rotate:9,7,5,7,4,7,8,7,2,7
Rotating Input String:
9,7,5,7,4,7,8,7,2,7
,7,5,7,4,7,8,7,2,79
7,5,7,4,7,8,7,2,79,
,5,7,4,7,8,7,2,79,7
5,7,4,7,8,7,2,79,7,
,7,4,7,8,7,2,79,7,5
7,4,7,8,7,2,79,7,5,
,4,7,8,7,2,79,7,5,7
4,7,8,7,2,79,7,5,7,
,7,8,7,2,79,7,5,7,4
7,8,7,2,79,7,5,7,4,
,8,7,2,79,7,5,7,4,7
8,7,2,79,7,5,7,4,7,
,7,2,79,7,5,7,4,7,8
7,2,79,7,5,7,4,7,8,
,2,79,7,5,7,4,7,8,7
2,79,7,5,7,4,7,8,7,
,79,7,5,7,4,7,8,7,2
79,7,5,7,4,7,8,7,2,