Jetez un oeil à ce code, voilà comment je l'ai mis en place il (rappelez-vous #include <stdbool.h>
ou il ne fonctionnera pas):
for(i = 0; i < string_length; i++)
{
if(sentence[i] == sentence[string_lenght-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
Faire que il vérifiera si votre phrase est palindrome et, à la première occurence ce n'est pas vrai, il va casser la boucle for. Vous pouvez utiliser quelque chose comme
if(palindrome)
printf(..);
else
printf(..);
pour une simple invite pour l'utilisateur.
Exemple:
radar
est palindrome
abba est palindrome
abcabc ne palindrome
S'il vous plaît, faites attention au fait que
Abba
n'a pas été reconnu comme un palindrome en raison du fait que 'A' et 'a' ont différents codes ASCII:
'A' a la valeur de 65
' a 'a la valeur de 97 selon le ASCII table. Vous pouvez en savoir plus here.
Vous pouvez éviter ce problème en transformant tous les caractères de la chaîne en caractères minuscules. Vous pouvez le faire, y compris la bibliothèque <ctype.h>
et appelant la fonction int tolower(int c);
comme ça:
for (; *p; ++p) *p = tolower(*p);
ou
for(int i = 0; str[i]; i++){
str[i] = tolower(str[i]);
}
par code Earlz, jetez un oeil à this Q&A de regarder plus profondément dans cela.
EDIT: J'ai fait un programme simple pour ce faire, voir si elle peut vous aider à
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
void LowerCharacters(char *word, int word_lenth);
int main(void){
char *word = (char *) malloc(10);
bool palindrome = false;
if(word == 0)
{
printf("\nERROR : Out of memory.\n\n");
return 1;
}
printf("\nEnter a word to check if it is palindrome or not : ");
scanf("%s", word);
int word_length = strlen(word);
LowerCharacters(word,word_length);
for(int i = 0; i < word_length; i++)
{
if(word[i] == word[word_length-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
palindrome ? printf("\nThe word %s is palindrome.\n\n", word) : printf("\nThe word %s is not palindrome.\n\n", word);
free(word);
return 0;
}
void LowerCharacters(char *word, int word_length){
for(int i = 0; i < word_length; i++)
word[i] = tolower(word[i]);
}
Entrée:
Entrez un mot pour vérifier si elle est palindrome ou non: rADAR
sortie:
Le mot radar est le palindrome.
http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Biffen
Je n'arrive toujours pas à trouver le problème –
Vous ne nous montrez pas assez de code. Qu'est-ce que IntCtr2? À quoi sont-ils initialisés IntCtr et IntStrlength? – Biffen