Programme Palindrome en C

Mon programme en C qui est Palindrome a une erreur dans sa fonction. Ma fonction ne compare pas les 2 caractères de ma chaîne. Lorsque je tape un seul caractère, il répond à palindrome mais s’il en existe deux ou plus, ce n’est pas toujours palindrome.

Code:

int IntStrlength=strlen(StrWord); int IntCtr2=0; int IntCtr=1, IntAnswer; while(IntCtr<=(IntStrlength/2)){ printf(" %d %d\n", IntCtr2,IntStrlength); if(StrWord[IntStrlength] != StrWord[IntCtr2]){ IntAnswer=0; printf(" %d=Not Palindrome", IntAnswer); exit (0); }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <--------- else{ IntCtr2++; IntStrlength--; }// else <-------- IntCtr++; }//while(IntCtr<IntStrlength/2) <----------- IntAnswer=1; printf(" %d=Palindrome", IntAnswer); return ; 

}

Personnage unique:

Deux personnages ou plus:

Pourquoi ne pas l’écrire comme ça

 int wordLength = strlen(StrWord); for (int i=0;i<(wordLength/2);i++) { if (StrWord[i] != StrWord[wordLength-i-1]) { return 0; } } return 1; 

Pour les mots de longueur égale (disons 8), le compteur passera de 0 à 3, accédant à toutes les lettres. Pour les mots inégaux (par exemple 7), le compteur passera de 0 à 2, laissant l’élément du milieu non contrôlé. Ce n'est pas nécessaire car c'est un palindrome et il correspond toujours

J’ai déjà vu cet algorithme dans un livre d’interviews intitulé “Cracking the Coding Interview”.

L’auteur y montre une implémentation très simple et facile du code. Le code est ci-dessous: Voici également une vidéo expliquant le code.

 #include #include // strlen() void isPalindrome(char str[]); int main(){ isPalindrome("MOM"); isPalindrome("M"); return 0; } void isPalindrome(char str[]){ int lm = 0;//left most index int rm = strlen(str) - 1;//right most index while(rm > lm){ if(str[lm++] != str[rm--]){ printf("No, %s is NOT a palindrome \n", str); return; } } printf("Yes, %s is a palindrome because the word reversed is the same \n", str); } 
 #include int check_palindrom(char *); int main() { char s1[20]; printf("Enter the ssortingng...\n"); gets(s1); int x; x=check_palindrom(s1); x?printf("Palindrom\n"):printf("Not Palindrom\n"); } int check_palindrom(char *s) { int i,j; for(i=0;s[i];i++); for(i=i-1,j=0;i>j;i--,j++) if(s[i]!=s[j]) return 0; if(s[i]==s[j]) return 1; } 

Entrez la chaîne …

radar

Palindrom

Vous pouvez faire ceci comme ceci:

 #include  #include  int check_palindrome(char ssortingng []); int main() { char ssortingng[20]; printf("Enter the ssortingng...\n"); scanf ("%s", &ssortingng); int check; check = check_palindrome (ssortingng); if (check == 0) printf ("Not Palindrome\n"); else printf ("Palindrome\n"); return 0; } int check_palindrome (char ssortingng []) { char duplicate []; strcpy (ssortingng, duplicate); strrev (ssortingng); if (strcmp (ssortingng, duplicate) == 0) return 1; else return 0; } 

Ceci utilise les fonctions strcmp et strrev .

Regardez ce code, c’est comme ça que je l’ai implémenté (rappelez-vous de #include ou cela 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 cela vérifiera si votre phrase est palindrome et, à la première occurrence, ce n'est pas vrai, cela cassera la boucle for. Vous pouvez utiliser quelque chose comme

 if(palindrome) printf(..); else printf(..); 

pour une simple invite pour l'utilisateur.

Exemple :

le radar est palindrome

Abba est palindrome

abcabc n'est pas palindrome

S'il vous plaît, faites attention au fait que

Abba

n'est pas reconnu comme un palindrome car «A» et «a» ont des codes ASCII différents:

'A' a la valeur de 65

"a" a la valeur 97 selon la table ASCII . Vous pouvez en savoir plus ici .

Vous pouvez éviter ce problème en transformant tous les caractères de la chaîne en caractères minuscules. Vous pouvez le faire en incluant la bibliothèque et en 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]); } 

Code par Earlz , jetez un coup d’œil à cette Q & R pour approfondir ce sujet.

EDIT: J'ai fait un programme simple pour faire cela, voir si cela peut vous aider

 #include  #include  #include  #include  #include  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]); } 

Consortingbution :

Entrez un mot pour vérifier s’il s’agit d’un palindrome ou non: RadaR

Sortie:

Le mot radar est palindrome.