varunesh.88
BAN USER- 0of 0 votes
AnswersWrite a function to check whether the two strings are rotation of each other or not.
Example: str1="Password" str2="ordPassw"
My Solution was.........
- varunesh.88 in India#include<stdio.h> void fn(char * str1, char * str2) { int i=0, j=0,flag=0; while(str2[i]) { if(str2[i]== *str1) { flag=1; break; } i++; } if(!flag) { printf("2nd String is not a rotation of other\n"); return; } j=i; while(str2[i]) { if( !(str2[i]==*str1)) { printf("2nd String is not a rotation of other\n"); return; } i++; str1++; } i=0; while(i<j) { if( !(str2[i]==*str1)) { printf("2nd String is not a rotation of other\n"); return; } i++; str1++; } if(*str1=='\0') printf("String are rotation of each other\n"); else printf("2nd String is not a rotation of other\n"); } int main() { char * str1="Password"; char * str2="ordPassw"; fn(str2, str1); return 0; }
| Report Duplicate | Flag | PURGE
Morgan Stanley Intern C
murti we have ask to write a function, I think using issubstr(s1,s2) is not a feasible solution.....your comments are welcome.....
- varunesh.88 December 07, 2012my solution was..
void fn(char * str)
{
int i=0;
int a[26]={0};
while(str[i])
a[str[i++]-'a']++;
i=0;
while(str[i])
{
if(a[str[i++]-'a']==1)
{
printf("First non repeating character is %c\n", str[i-1]);
break;
}
}
if(str[i]=='\0')
printf("All characters are repeating");
}
Repnnatashanaomi, Android test engineer at 247quickbookshelp
I am Marketing managers who play a crucial role in helping a business to promote and its customers. I manage ...
Bin.... your code will not give the first non repeating character in the srting.... But it gives any non repeating character from the string which may or may not be the first.... Check it onces..
- varunesh.88 December 08, 2012