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.

Remove duplicate characters in string

Write code to remove the duplicate characters in a string
without using any additional buffer NOTE: One or two additional variables are fine
An extra copy of the array is not.
This question is from the book “cracking thecoding interview”.

Time complexity= O(n^2)

Leave comment if you have any problem with code.
Recursive insertion sort