0
I have to do a show and almost finished the same.
Here’s what you ask for:
Palindrome is a phrase or word that keeps the same meaning when read backwards, for example bone, he, grate, etc. Make a program that reads a word of no more than 20 characters and print out whether this word is a palindrome or not.
This function should return 1 if the word is a palindrome, or 0, otherwise.
Input: A string. Output: Palindrome or Nao is a palindrome.
The problem is that the code reverses the string, but something is missing to fix and effectively compare the strings str and test
Used libraries (stdio.h
, stdlib.h
and string.h
)
char str[20],teste[20]; // teste = cópia da str p/ testar condição palíndromo
int a,i,j,tam = 0;
scanf("%s",str);
for (i = 0, j = strlen(str); str[i] !='\0'; i++,j--){
teste[j-1] = str[i];
tam++;
}
teste[j-1] = '\0';
for(i = 0; i < tam; i++){
printf("%c",teste[i]); // imprime normalmente a string invertida na tela
}
printf("\n");
printf("%s\n",teste); // saída da string teste, porém nessa saída é possível ver que há mais caracteres inclusos na string e assim, invalidam a comparação que deverá ser feita.
return 0;
}
This is the final part of the code to be put in place of the printf("%s\n",teste);
second-to-last line.
if(strcmp (str, teste) == 0){
printf("Palindromo");
}else{
printf("Nao e um palindromo");
}
I anticipate my thanks to those who can help!
This code is confusing, inefficient and does things that the statement cannot. The question already answered has a much simpler form.
– Maniero
Oops thanks. I’ve seen the algorithm used there. Thanks for the help :)
– Misael