Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- typedef enum {
- ORIGIN = 'o', LOW = 'l', UP = 'u', CAP = 'c'
- } ProgModesE;
- FILE *openFile(char path[], char mode[]) {
- FILE *f = NULL;
- if ((f = fopen(path, mode)) == NULL) {
- perror("");
- exit(-1);
- }
- return f;
- }
- void closeFile(FILE *f) {
- if (fclose(f) == EOF) {
- perror("");
- }
- }
- void readNWrite(ProgModesE mode, FILE *fin) {
- for (int ch, last = 0; (ch = fgetc(fin)) && !feof(fin); last = ch) {
- switch (mode) {
- case ORIGIN: break;
- case LOW:
- ch = tolower(ch);
- break;
- case UP:
- ch = toupper(ch);
- break;
- case CAP:
- if (!isalnum(last) && isalpha(ch)) {
- ch = toupper(ch);
- } else {
- ch = tolower(ch);
- }
- break;
- default:
- fprintf(stderr, "error!\n");
- }
- putchar(ch);
- }
- }
- int main(int argc, char **argv) {
- FILE *fin = NULL;
- ProgModesE mode = ORIGIN;
- for (int i = 1; i < argc; i++) {
- if (*argv[i] == '-') {
- mode = (ProgModesE)argv[i][1];
- } else {
- fin = openFile(argv[i], "r");
- readNWrite(mode, fin);
- }
- }
- if (fin != NULL) {
- closeFile(fin);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement