Trying to write that function checks whether all the letters in word appear in s, in the same order. If a letter appears several times in word, then it should appear
at least as many times in s.
For example:
containsLetters2("abcdef", "aaabbb")
returns 0 because there are no three appearances of the letter "a" followed by three appearances of the letter “b”.
This:
containsLetters2("axaxxabxxbxbcdef","aaabbb")
returns 1
I can't understand whats wrong in my code:
int containsLetters2(char *s, char *word)
{
int j,i, flag;
long len_word, len_s;
len_s=strlen(s);
len_word=strlen(word);
for (i=0; i<=len_word; i++) {
flag=0;
for (j=0; j<=len_s; j++) {
if (*word==*s) {
flag=1;
word++;
break;
}
s++;
}
if (flag==0) {
break;
}
}
return flag;
}
int main() {
char string3[MAX_STRING] , string4[MAX_STRING];
printf("Enter 2 strings for containsLetters2\n");
scanf ("%s %s", string3, string4);
printf("Return value from containsLetters2 is: %d\n",containsLetters2(string3,string4));
return 0; }
"aababbb", "aaabbb"? There are 3 a's followed by 3 b's; does the intermediate b prevent the match working?