1
I need to check whether a string The, for example, is equal to string B to determine if the word inserted is a palindrome, the problem is that a string B is the reverse of A, I’m not able to determine if the contents are equal. For example, I enter the word "macaw", the program should show a message that the word is a palindrome, instead it shows the message that says the word is not a palindrome.
#define N 20
int main()
{
char palavra_a[N], palavra_b[N];
int i,j=0,flag=0;
puts("\nInsira uma palavra: \n");//pegando a palavra
fflush(stdin);
gets(palavra_a);
for(i=strlen(palavra_a);i>0;i--)
{
palavra_b[j]=palavra_a[i];
j=j+1;
}
for(i=0;i<strlen(palavra_a)-1;i++)
{
if(palavra_a[i]==palavra_b[i])
{
flag=0;
printf("%c",palavra_b[i]);
}
else
if(palavra_a[i]!=palavra_b[i])
{
flag=1;
break;//caso a letra da palavra b seja diferente da palavra a, o laço é quebrado
}
}
printf("\n %s",palavra_b[N]);
switch (flag)
{
case 0:
puts("\nA palavra inserida eh um palindromo.");
break;
case 1:
puts("\nA palavra inserida nao eh um palindromo.");
break;
}
system("Pause");
return 0;
}
use the function
strcmp
, in case you haven’t loaded it, do#include <string.h>
– Edilson
but I need to copy backwards to check if the word is palindroma
– soAna
what he meant is after copying it backwards, use only strcmp() to compare. strcmp(palavra_a, palavra_b)
– Henrique Santiago
By your code you see something different, because by what you’re now saying in case it would be, uncouple, reverse, regroup and finally compare.
– Edilson