I pass 3 strings to the function and need look through the strings to find a word that appears in all three of them. The strings are just letters with no spaces or interpunction. I wrote a code to go through 2 strings but it doesn’t work for some reason. Any help is appreciated.
char* najduljiPodniz(char* niz1, char* niz2, char* niz3)
{
int i,j,t1,br=0;
for(i=0;i<strlen(niz1);i++)
{
for(j=0;j<strlen(niz2);j++)
{
if(niz1[i]==niz2[j])
{
t1=i;
br++;
while(niz1[i]==niz2[j])
{
br++;
i++;
j++;
}
}
else break;
}
}
char *podniz=(char*)malloc(sizeof(char)*br+1);
for(i=0,j=t1;j<br;i++,j++)
podniz[i]=niz1[j];
for(i=0;i<br;i++)
printf("%s",podniz[i]);
return 0;
}
For clarification: Example of a string would be: “afsdmartiangknrhg”. So, random letters and somewhere in the string there’s an actual word. In this example “martian”. The other 2 strings also contain the word “martian”. The word martian is “unkown” to me so I can’t check for that actual word in the string.
OK, here’s my new answer based on the revised description of the problem (see the comments on my first answer).
First, choose the shortest of the three strings. Iterate over all possible substrings of this string, starting with the longest. For each substring, use
strstrto search for it in the other two strings. If it’s found in both, you have your solution.Try something like: