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?
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 characterstr[i]
can bea
ors
not both simultaneously would not beif((str[i] == 'a') || (str[i] == 's'))
?– Augusto Vasques
You could click the " " of Manero’s answer, right? He gave the correct answer.
– RHER WOLF
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).
– Maniero