Bookmark and Share

free counters
Back to Problem list
Problem 27: 
You are required to rotate a word a certain amount. For example, to rotate the word "Computer"
by 1 results in "rCompute".Rotating it two more times gives you "terCompu".
Input: The first line contains the word (which will not have more than 15 letters).
The second line contains an integer n, which will be less than the length of the word.
You must rotate the word n times. Output: The output will be the rotated word. 

Example:
Input:

Computer
3

Output:
terCompu


#include<stdio.h>
#include<string.h>

void Rotate(char input[], int n) {
    int i, len = strlen(input), j;
    if(n > len) {
        printf("Invalid input\n");
        return;
    }
    for(i = len-n; i<len; i++) { // printing last n character
        printf("%c",input[i]);
    }
    for(i = 0; i<len-n; i++) { 
        printf("%c",input[i]);
    }
    printf("\n");
    
}
int main() {
    char input[100];
    int n;
    scanf("%s",input);
    scanf("%d",&n);
    Rotate(input, n);
}


Back to Problem list