saharz
saharz

Reputation: 87

Strings (C) - Comparing letters of two strings

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; }

Upvotes: 0

Views: 122

Answers (3)

Daniel
Daniel

Reputation: 2185

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;
    }
}

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182875

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.

Upvotes: 2

ichramm
ichramm

Reputation: 6642

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;
}

Upvotes: 0

Related Questions