Can’t find one string inside the other

Asked

Viewed 81 times

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.

  • Thank you very much! I changed the FGETS function by GETS and it worked.

  • It may have worked but it’s not the recommended option.

  • Avoid gets(), is obsolete. Can replace with padrao[strlen(padrao) - 1]=0; strlen also counts '\n' as any character.

  • @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).

1 answer

2

The main problem is of algorithm itself. The loop does not make the slightest sense in the written form. There is also a syntax error in the code and so it does not compile and is poorly organized. I made a change to scanf() only to rotate. The fgets() has its own way of putting together the die, but this is another side issue to the question (example how to do), then you can fix it in your code. But don’t use the gets() (the solution used in fgets() It’s bad for performance, but it’s what’s ready).

The tie didn’t count for anything, it came out at the first execution, and in fact if it didn’t come out it would do infinite repetition because nothing increased it.

#include <stdio.h>
#include <string.h>

int main () {
    char padrao[52], mensagem[52];
    printf("Digite uma palavra:");
    fgets(mensagem, 51, stdin);
    printf("Digite o padrao a ser encontrado:");
    scanf("%s", padrao);
    printf("Palavra digitada: %s", mensagem);
    printf("Padrao digitado: %s", padrao);
    int aux = 0;
    char *tmp = mensagem;
    while ((tmp = strstr(tmp, padrao))) {
        aux++;
        tmp++;
    }
    if (aux > 0) printf("****Padrao encontrado %d vezes!****", aux);
    else printf("****Padrao nao encontrado!****");
}

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

Browser other questions tagged

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