1 #include<stdio.h> 2 #include<stdlib.h> 3 char * 4 sous_chaine (const char *s, unsigned int debut, unsigned int fin) 5 { 6 char *new_ch = NULL; 7 8 if (s != NULL && debut < fin) 9 { 10 /* (1) */ 11 new_ch = malloc (sizeof (*new_ch) * (fin - debut + 2)); 12 if (new_ch != NULL) 13 { 14 int i; 15 16 /* (2) */ 17 for (i = debut; i <= fin; i++) 18 { 19 /* (3) */ 20 new_ch[i - debut] = s[i]; 21 } 22 } 23 else 24 { 25 fprintf (stderr, "Memoire insuffisante\n"); 26 exit (-1); 27 } 28 } 29 return new_ch; 30 } 31 32 char * 33 mafonc (char chaine[]) 34 { 35 int i; 36 char* buffer = malloc((strlen(chaine)+1) * sizeof(*buffer)); 37 fprintf (stdout, "chaine avant traitement=%s\n", chaine); 38 39 for (i = strlen (chaine); i >= 0; i--) 40 { 41 if (chaine[i] == ',') 42 break; 43 } 44 sprintf ( buffer, "%s et %s", sous_chaine(chaine,0, i-1), sous_chaine(chaine,i+1,strlen (chaine))); 45 return ( buffer); 46 } 47 48 int 49 main (void) 50 { 51 fprintf (stdout, "chaine apres traitement=%s\n", mafonc ("1,2,3,4")); 52 fprintf (stdout, "chaine apres traitement=%s\n", mafonc ("1,2,3,4,5,6,7,8,9,...,2007")); 53 return (0); 54 } 55 56