Interview Questions

211. Write an algorithm which will return position of first character of string

Microsoft Interview Questions and Answers


(Continued from previous question...)

211. Write an algorithm which will return position of first character of string

Question:
String s1 = bacdeg
String s2 = bcdaeg

Write an algorithm which will return position of first character of string s1 that is appearing in s2
here postion of 'a' in string s2 as it is the first character of s1 that is coming in s2


maybe an answer:


according to the question 'b' appears first in both s1 and s2
int fn(s1,s2)
{
char a,b;
int i=j=0;
while(s1)
{
a=s1[i++];
while(s2)
{
j=0;
b=s2[j++];
if (a==b)
retun j;
break;
}
}
return -1;
}

(Continued on next question...)

Other Interview Questions