Advertisement
STANAANDREY

harder lab14 ex

Feb 4th, 2023 (edited)
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. typedef enum {
  6.   ORIGIN = 'o', LOW = 'l', UP = 'u', CAP = 'c'
  7. } ProgModesE;
  8.  
  9. FILE *openFile(char path[], char mode[]) {
  10.   FILE *f = NULL;
  11.   if ((f = fopen(path, mode)) == NULL) {
  12.     perror("");
  13.     exit(-1);
  14.   }
  15.   return f;
  16. }
  17.  
  18. void closeFile(FILE *f) {
  19.   if (fclose(f) == EOF) {
  20.     perror("");
  21.   }
  22. }
  23.  
  24. void readNWrite(ProgModesE mode, FILE *fin) {
  25.   for (int ch, last = 0; (ch = fgetc(fin)) && !feof(fin); last = ch) {
  26.     switch (mode) {
  27.     case ORIGIN: break;
  28.     case LOW:
  29.       ch = tolower(ch);
  30.       break;
  31.     case UP:
  32.       ch = toupper(ch);
  33.       break;
  34.     case CAP:
  35.       if (!isalnum(last) && isalpha(ch)) {
  36.         ch = toupper(ch);
  37.       } else {
  38.         ch = tolower(ch);
  39.       }
  40.       break;
  41.     default:
  42.       fprintf(stderr, "error!\n");
  43.     }
  44.     putchar(ch);
  45.   }
  46. }
  47.  
  48. int main(int argc, char **argv) {
  49.   FILE *fin = NULL;
  50.   ProgModesE mode = ORIGIN;
  51.   for (int i = 1; i < argc; i++) {
  52.     if (*argv[i] == '-') {
  53.       mode = (ProgModesE)argv[i][1];
  54.     } else {
  55.       fin = openFile(argv[i], "r");
  56.       readNWrite(mode, fin);
  57.     }
  58.   }
  59.   if (fin != NULL) {
  60.     closeFile(fin);
  61.   }
  62.   return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement