Check whether one string is rotated version of other(Josh software programming test problem-2)

Write a program to accept two strings str1 and str2 and checks whether str2 is rotated version of str1.
Ex. Following are different values of str2 for given str1.
GIVEN:str1=”software”
2-character left rotate
str2=’resoft’
4-character left rotate
str2=’waresoft’
3-character right rotate
str2=’twaresof’

It seems to be bit difficult problem ,but it’s not actually. To solve this problem , We have to append first string to itself. And then check whether second string is present in first or not.

For example:
str1=”software”
str2=”resoft”
so append str1 to itself. Hence now str1=”softwaresoftware” . Now check str2 is present in str1 or not. Str2 is present in str1, hence str2 is rotated version of str1.

Note- You can not use strcat() function of string.h to concatenate a string to itself .Concatenation of string to itself using strcat() function is undefined behavior in C.

Solution using inbuilt  function of C.

 

Solution without using inbuilt functions

Josh Software programming test Problem 1

Leave comment if you have any problem with code.

About Gaurav Vichare

Hi , I am Gaurav : author of this blog , Software engineer and a Photoshop lover . You can connect with me on Facebook , cirlce me on Google or follow on Twitter

  • Kunal Vichare

    No new Programs from many days

    • Yes, I was busy in last few months. Now I started blogging once again. Checkout my new post!
      My next few posts will be on performance measurement of python code and web2py application.