Why does my running function only return "0"?

Asked

Viewed 104 times

0

I’m doing a function that gets a string (char str[10]) and she has to return 1 if to string contain the word "wing" and otherwise return 0.

To make it easier to understand what I tried to do, I’ll show you an example:

0 1 2 3 4 5 6 7 8 9

x s a s a t i \0

In this case, the function has to return 1 because the word "wing" is contained in the string received (positions 2, 3 and 4).

The code I made:

#include <stdio.h>
int vetorPalavra(char str[10]){
    int i, cont;
    for(i=0;i<10;i++){
        if((str[i] == 'a') && (str[i] == 's')){
            return 1;
        }
        else{
            return 0;
        }
    }
}
main(){
    fflush(stdin);
    char str[10] = {'x','d','m','a','s','a','h','k','p','g'};
    printf("%i", vetorPalavra(str));
}

In function main(), note that I have already entered the values of the vector. Then it is not necessary to insert anything when executing. It will already display the direct value (0 or 1). However, when executing it only shows the value 0, when it was to show 1. What I did wrong?

Resultado

I’ve tried to use fflush(stdin), but it didn’t do any good. I thought about using an accountant but I don’t think this exercise would work.

  • if((str[i] == 'a') && (str[i] == 's')) the character str[i] can be a or s not both simultaneously would not be if((str[i] == 'a') || (str[i] == 's')) ?

  • You could click the " " of Manero’s answer, right? He gave the correct answer.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

4

Failed to compare with the letter a and walk on the index in each letter. The a in an element of array, to be valid the next element should be s, and the following must be a. The question code is comparing to the same element, which is impossible for it to be two different things at the same time, but it’s not even that intention, it has to compare with three elements in sequence at a time.

Another problem is that if he does not find at the first opportunity closes the search saying that he does not have, and this should only happen when he searches throughout the string, otherwise you wouldn’t even need a bow.

It’s all about interpreting the problem correctly and translating it into code.

This algorithm only works with strings formed correctly and with the expected size (can have a small variation), according to what is said in the statement. The code does not create a string formed correctly. To make it work with a array of characters that is not a string the search must end 2 characters before to not pick up memory junk to compare. As optimization the new code already does this.

Organizing and fixing other minor problems:

#include <stdio.h>

int vetorPalavra(char str[10]) {
    for (int i = 0; i < 7; i++) if (str[i] == 'a' && str[i + 1] == 's' && str[i + 2] == 'a' ) return 1;
    return 0;
}
int main() {
    printf("%d", vetorPalavra("xdmasahkp"));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Just out of curiosity whether the comparison should really be just with the beginning of string and not in all of it, the function could have only this line:

return str[0] == 'a' && str[1] == 's' && str[2] == 'a';
  • Why not error if you don’t have asa in the word and the last character be a? I thought I was going to give out of range on i+1 and i+2

  • There is no out of range in C. Read the 4th. paragraph.

  • #include <stdio. h> int vectorPalavra(char str[10]){ int i; for(int i = 0;i<9;i++){ if(str[i] == 'a') && (str[i + 1] == ’s') && (str[i + 2] == 'a'){ Return 1; } Else{ Return 0; } } } main(){ printf("%i", vectorPalavra("xdmasahkp"); }

  • Do not: https://ideone.com/GLw0hB

  • No, that’s how it’s done. Yours isn’t like mine, if you copy and paste it will work. If you read the answer you will learn how to do and why yours doesn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.