Vous devez vous connecter pour exécuter votre code.

fcopy_charcond — Copie conditionnelle (É 6.2)

É 6.2 — Écrivez fcopy_charcond qui copie uniquement les caractères satisfaisant une condition (pointeur de fonction).

#include <stdio.h> #include <ctype.h> int fcopy_charcond(const char *destfn, const char *srcfn, int (*charcond)(int)) { FILE *src = fopen(srcfn, "r"); if (src == NULL) return -1; FILE *dst = fopen(destfn, "w"); if (dst == NULL) { fclose(src); return -1; } int c; while ((c = fgetc(src)) != EOF) { if (charcond(c)) { if (fputc(c, dst) == EOF) { fclose(src); fclose(dst); return -1; } } } int err = ferror(src) ? -1 : 0; fclose(src); fclose(dst); return err; } int main(int argc, char *argv[]) { if (argc < 3) return 1; printf("%d\n", fcopy_charcond(argv[1], argv[2], isalpha)); return 0; }