2016-01-14 1 views
-5

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. Quand je tape un seul caractère, il répond palindrome mais si c'est deux ou plus, toujours pas palindrome.Programme Palindrome en C

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 ; 

}

caractère unique:

Deux ou plusieurs personnages:

+2

http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Biffen

+0

Je n'arrive toujours pas à trouver le problème –

+1

Vous ne nous montrez pas assez de code. Qu'est-ce que IntCtr2? À quoi sont-ils initialisés IntCtr et IntStrlength? – Biffen

Répondre

1

Pourquoi ne pas é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 avec une longueur même (disons 8), le compteur passera de 0 à 3, l'accès à toutes les lettres. Pour les mots inégaux (disons 7), le chiffre passera de 0 à 2, laissant l'élément central non coché. Ce n'est pas nécessaire puisque son palindrome et il correspond toujours à se

+0

Cela fonctionne. mais pourquoi mon code ne fonctionne pas? merci –

+0

La première fois que vous exécutez la boucle wile, cette vérification est effectuée: if (StrWord [IntStrlength]! = StrWord [IntCtr2]) IntStrlength est basé sur 1, pas sur 0. donc le caractère de fin de la chaîne sera obtenu et comparez-le à la première lettre. cette vérification échouera toujours. vous devez utiliser IntStrlength-1 – Houbie

0

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.

0
#include<stdio.h> 
int check_palindrom(char *); 
int main() 
{ 
     char s1[20]; 
     printf("Enter the string...\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

0

Vous pouvez le faire comme ceci:

#include <stdio.h> 
#include <string.h> 

int check_palindrome(char string []); 
int main() 
{ 
     char string[20]; 
     printf("Enter the string...\n"); 
     scanf ("%s", &string); 

     int check; 
     check = check_palindrome (string); 
     if (check == 0) 
      printf ("Not Palindrome\n"); 
     else 
      printf ("Palindrome\n"); 
     return 0; 
} 
int check_palindrome (char string []) 
{ 
     char duplicate []; 
     strcpy (string, duplicate); 
     strrev (string); 
     if (strcmp (string, duplicate) == 0) 
      return 1; 
     else 
      return 0; 
} 

Ceci utilise la fonction strcmp et strrev.