Why are you printing strange character at the end of the palindrome evaluation?

Asked

Viewed 55 times

-2

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    char frase[200], vetor_frase[200], palindromo[200];
    int j = strlen(frase), x = 0, p =0;

    printf("Digite uma frase:");
    gets(frase);
    for(int i = 0; i< j; i++)
    {
        if(frase[i] != ' ')
        {
            vetor_frase[x] = tolower(frase[i]);
            x++;
        }
    }
    for(int i= strlen(vetor_frase)-1; i>=0; i-- )
    {
        palindromo[p] = vetor_frase[i];
        p++;
    }
    int comparacao = strcmp(vetor_frase, palindromo);
    if (comparacao == 0)
    {
        printf(">A frase %s \n E UM PALINDROMO:%s",vetor_frase, palindromo);
    }
    else
    {
        printf("A frase %s \nNAO E PALINDROMO:%s",vetor_frase, palindromo);
    }


    return 0;
}
  • In C a string is an array of characters with the terminator ' 0'. You have not typed this terminator at the end of the sentence vector mount. Idem for palindromo.

  • That’s right brother. vlw!.

1 answer

0

Try to put the function strlen() after the gets(frase), because doing this before reading the input causes the function to capture a wrong length.

  • i edited wrong brother, at the time of putting here. but vlw!. was pq I was forgetting the lock of my character vector.

Browser other questions tagged

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