0

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; }
3
  • it should be like this in for loop --> 1)--> i<len_word 2)--> j<len_s Commented Jan 9, 2014 at 6:19
  • See also Pointers to String C for a related, but distinct, question. Commented Jan 9, 2014 at 6:33
  • How would you handle "aababbb", "aaabbb"? There are 3 a's followed by 3 b's; does the intermediate b prevent the match working? Commented Jan 9, 2014 at 6:35

3 Answers 3

2
for (i=0; i<=len_word; i++) {
    flag=0;
    for (j=0; j<=len_s; j++) {
        if (*word==*s) {

You have two obvious problems. One is an off by one error. If the length is 10, then your code processes elements from 0 to 10, which is 11 elements, not 10. Second, you keep comparing *word to *s. What you want is word[i] compared to s[j].

There are lots more problems that are not as obvious. I'd strongly suggest you take a step back and start by documenting the algorithm your code is supposed to follow. That will make it easier to debug the code as you will know precisely what it is supposed to be doing.

Sign up to request clarification or add additional context in comments.

Comments

0

The following code may not compile, but will do what you want it to do, hope it helps:

int containsLetters2(char *s, char *word) {
    int lastIndex = 0;
    for (int i = 0; i < strlen(word); i++) {

        for (; lastIndex < strlen(s) && s[lastIndex] != word[i]; lastIndex++);

        if (lastIndex == strlen(s)) {
            return 0;
        }
    }
    return 1;
}

Comments

0

You should preserve the value of variable j, For example,

word = "aaabbb"
s = "axaxxabxxbxbcdef"

The 1st iteration, word[1] == s[1], then it break and come to the 2nd iteration, at this time, j is reset to zero, and word[2] == s[1].

This got to be wrong. Change your code as below to see if it helps,

i=j=0;
for (; i<len_word; i++) {
    flag=0;
    for (; j<len_s; j++) {
        if (*word==*s) {
            flag=1;
            word++;
            break;
        }
        s++;

    }

    if (flag==0) {
        break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.