/* * Program to copy text from input to output, changing case of letters. * * Input: text file "in.txt". * * Output: text file "out.txt", contents the same as "in.txt" except that * uppercase letters have been changed to lowercase, and vice versa. */ #include #include #include int main(void) { FILE * in; FILE * out; char input_char; /* open input file and check result */ in = fopen("in.txt", "r"); if (in == NULL) { printf("unable to open input file\n"); /* exit program immediately */ exit(1); } /* open output file and check result */ out = fopen("out.txt", "w"); if (out == NULL) { /* exit program immediately */ printf("unable to open output file\n"); exit(1); } /* read characters and copy to output, changing case */ while (fscanf(in, "%c", &input_char) == 1) { /* if c is uppercase, convert to lowercase and write */ if(isupper(input_char)) { fprintf(out, "%c", tolower(input_char)); } /* if c is lowercase, convert to uppercase and write */ else if (islower(input_char)) { fprintf(out, "%c", toupper(input_char)); } /* otherwise just write */ else { fprintf(out, "%c", input_char); } } /* close files */ fclose(in); fclose(out); return 0; }