#include #include #include #include #define TAILLE_TAMPON 256 char * chaine_copier(const char *s){ char *copie = (char *) malloc( (strlen(s)+1) * sizeof(char)); assert(copie != NULL); return strcpy(copie,s); } char * chaine_n_copier(const char *s, int n){ char *copie = (char *) malloc( (n+1) * sizeof(char)); assert(copie != NULL); strncpy(copie,s,n); copie[n] = '\0'; return copie; } char * chaine_concatener(const char *s1, const char *s2){ char *copie = (char *) malloc( (strlen(s1) + strlen(s2) + 1) * sizeof(char)); assert(copie != NULL); strcpy(copie,s1); return strcat(copie,s2); } char * lire_chaine(){ char tampon[TAILLE_TAMPON]; scanf("%s",tampon); char *copie = (char *) malloc( (strlen(tampon)+1) * sizeof(char)); assert(copie != NULL); return strcpy(copie,tampon); } void usage(char * cmd){ fprintf(stderr,"Usage : %s \n",cmd); exit(EXIT_FAILURE); } int main(int argc, char *argv[]){ if (argc != 3) usage(argv[0]); char *s = chaine_copier(argv[1]); printf("L'argument de la commande est %s\n",s); printf("Saisir une chaine au clavier : "); char *s1 = lire_chaine(); char *s2 = chaine_concatener(s,s1); printf("La concatenation de %s et %s est %s\n",s,s1,s2); int n = atoi(argv[2]); char *s3= chaine_n_copier(s2,n); printf("La copie des %d premiers caracteres de %s est %s\n",n, s2, s3); free(s); free(s1); free(s2); free(s3); return EXIT_SUCCESS; }