Palyndrome
faaa
-
faaa -
faaa -
Bonjour,
J'aimerais SVP que vous jetiez un coup d'œil sur mon programme en C qui dit si un nombre est un palyndrome ou pas.
Merci d'avance.
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
int main ()
{
char *mot;
int i, ispalindrome(char *mot);
printf("Entrer un mot \n");
scanf("%s", &mot);
for(i=0; i<5; i++)
{
scanf("%s", &mot[i]);
printf("%s", mot[i]);
}
if (ispalindrome(mot)!=0)
printf("Ce mot est un palyndrome");
else
printf("Ce mot n'est pas un palyndrome");
return 0;
}
// si on ne tient pas compte de la ponctuation, ça donne :
int ispalindrome(char *chaine)
{
int l=strlen(chaine)-1;
int i;
for (i=0; i<=l/2; i++)
if (chaine[i]!=chaine[l-i])
return i+1;
return 0;
}
J'aimerais SVP que vous jetiez un coup d'œil sur mon programme en C qui dit si un nombre est un palyndrome ou pas.
Merci d'avance.
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
int main ()
{
char *mot;
int i, ispalindrome(char *mot);
printf("Entrer un mot \n");
scanf("%s", &mot);
for(i=0; i<5; i++)
{
scanf("%s", &mot[i]);
printf("%s", mot[i]);
}
if (ispalindrome(mot)!=0)
printf("Ce mot est un palyndrome");
else
printf("Ce mot n'est pas un palyndrome");
return 0;
}
// si on ne tient pas compte de la ponctuation, ça donne :
int ispalindrome(char *chaine)
{
int l=strlen(chaine)-1;
int i;
for (i=0; i<=l/2; i++)
if (chaine[i]!=chaine[l-i])
return i+1;
return 0;
}
2 réponses
tu obtiens une erreur segmentation fault n'est-ce-pas ?
scanf("%s", &mot[i]);
mot n'est pas un tableau de char, c'est un pointeur sur un char. Tu peux, si tu sais faire ça, créer dynamiquement ton tableau avec un malloc. Tu peux aller guigner dans cette FAQ aussi : https://c.developpez.com/faq/?page=Les-tableaux#TABLEAUX_tableau_parametre (cet article et d'autres ...)
scanf("%s", &mot[i]);
mot n'est pas un tableau de char, c'est un pointeur sur un char. Tu peux, si tu sais faire ça, créer dynamiquement ton tableau avec un malloc. Tu peux aller guigner dans cette FAQ aussi : https://c.developpez.com/faq/?page=Les-tableaux#TABLEAUX_tableau_parametre (cet article et d'autres ...)