1
When executing this code, and perform a test with:
Entrada: analise padrão
Mensagem: ana
The sentence is not found inside the other.
#include <stdio.h>
#include <string.h>
int main (){
char padrao [52], mensagem[52];
int aux=0;
printf("Digite uma palavra:");
fgest (mensagem,52,stdin);
printf("Digite o padrao a ser encontrado:");
fgets (padrao,52,stdin);
printf ("Palavra digitada: %s", mensagem);
printf ("Padrao digitado: %s", padrao);
while(strstr(mensagem, padrao)){
aux++; // se for verdadeiro, acrescenta em aux
printf("aux: %d\n", aux); //verificação
break;
}
if (aux > 0){
printf("****Padrao encontrado em %d mensagens!****", aux);
}else {
printf("****Padrao nao encontrado!****");
}
return 0;
}
Note that the fgets function stops reading when it detects a new line character ('n'), however this character will be part of the read string. Display the standard string length to make sure. A solution is for you to replace the character ' n' with the character ' 0' which indicates the end of the string. Probably your program would work for the Ise standard.
– anonimo
Thank you very much! I changed the FGETS function by GETS and it worked.
– Juan Santos
It may have worked but it’s not the recommended option.
– anonimo
Avoid
gets()
, is obsolete. Can replace withpadrao[strlen(padrao) - 1]=0;
strlen also counts'\n'
as any character.– Mateus -- O Schroeder
@Juansantos 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