1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <stdio.h>
#include <stdlib.h>
int g(unsigned int n, char *res){
if (n>9) return 1;
*res='0'+n;
return 0;
}
int f(unsigned int n, char *s, size_t *l){
char c; size_t cl=*l;
if(n == 0)
return 0;
g(n%10U, s);
c = *s;
++*l;
f(n/10U, s+1, l);
printf("*l = %d\n",*l);
printf("%c\n",c);
printf("cl = %d : *l/2 = %d\n", cl, *l/2);
if(cl<*l/2){
*s=s[*l-cl-1-cl];
s[*l-cl-1-cl]=c;
}
printf("-----");
printf("*s = %c\n",*s);
return 0;
}
int main(void){
int n;
char buf[BUFSIZ];/*Suppose tres grand*/
size_t l=0;
printf("Entrez un nombre: ");
if(scanf("%u",&n)==1)
{
if(n > 0)
f(n, buf, &l);
else if(n == 0)
{
buf[0]='\0';
++l;
}
else
{
fprintf(stderr,"erreur : il ne faut pas donner de valeur negative\n");
return EXIT_FAILURE;
}
buf[l]='\0';
fprintf(stdout, "%s %u\n", buf, l);
return EXIT_SUCCESS;
}
else return EXIT_FAILURE;
} |
Partager