Charlie has a magic mirror program in C

Charlie has a magic mirror program in C (Problem)

Charlie has a magic mirror. The mirror shows right rotated versions of a given word. To generate different right-rotations of a word. Write the word in a circle in clockwise order, then start reading from any given character in clockwise order till you have covered all the characters. In the word “sample”, if we start with ‘P’, we get the right rotated word as “plesam”. There are six such right rotations of “sample” including itself.

Input

The inputs to the function isSameReflection consist of two strings, word1, and word2

Output

function returns 1 if word1 and word2 are right rotations of the same word and -1 if they are not. Both word1 and word2 will strictly contain characters between ‘a’-‘z’ (lower case letters)

Example

Input: plesam

Output: 1

Charlie has a magic mirror program in C (Solution)

#include<stdio.h>
int main()
{
    int i,j;
    char s1[100],s2[100],s3[100];
    scanf("%s %s", s1,s2);
    for(i=0,j=0;s2[i]!='\0';i++,j++)
    {
        s3[j]=s2[i];
    }
    for(i=0;s2[i]!='\0';i++,j++)
    {
        s3[j]=s2[i];
    }
    s3[j]='\0';
    j=0;
    for(i=0;s3[i]!='\0';i++)
    {
        if(s3[i]==s1[j])
        {
            j++;
        }
        else
            j=0;
        if(s1[j]=='\0')
        {
            printf("1");
            break;
        }
    }
    if(s1[j]!='\0')
    {
        printf("-1");
    }
    return 0;
}

Conclusion

The goal of this tutorial is to solve Charlie’s Magic Mirror problem. Thanks!

Leave a Comment