Skip to content
Snippets Groups Projects
Select Git revision
  • bf4583cd77ebacd8ad01cbc3dd6ee2bef1f8b024
  • master default protected
2 results

palindrome_refactored.c

Blame
  • Forked from algorithmique / cours
    Source project has a limited visibility.
    palindrome_refactored.c 961 B
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    
    bool is_palindrome_1(char mot[]);
    bool is_palindrome_2(char mot[]);
    
    void main() {
       printf("Entrez une chaîne de caractères: ");
       char mot[30];
       scanf("%s",mot);
    
       printf("Le mot %s",mot);
       if (is_palindrome_1(mot)) {
          printf(" est un palindrome\n");
       } else {
          printf(" n'est pas un palindrome\n");
       }
    }
    
    bool is_palindrome_1(char mot[]) {
       int lg = strlen(mot);
       bool palindrome = true;
       int first_idx = 0, last_idx = lg-1;
       while (first_idx < last_idx) {
          if (mot[first_idx] != mot[last_idx]) {
             palindrome = false;
             break;
          } 
          first_idx += 1;
          last_idx  -= 1;
       }
       return palindrome;
    }
    
    bool is_palindrome_2(char mot[]) {
       int lg = strlen(mot);
       char inverse[lg+1];
       for (int i=0;i<lg;i++) {
          inverse[i] = mot[lg-1+i];
       }
       inverse[lg] = '\0';
       return (strcmp(inverse,mot) == 0); 
    }