Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. #define NUM_OF_LETTERS 'Z' - 'A'
  6.  
  7. struct Stats {
  8.   int white_characters;
  9.   int letter_counter[NUM_OF_LETTERS];
  10. };
  11.  
  12. struct Stats statistic;
  13.  
  14. int main(int argc, char* argv[]) {
  15.  
  16.   FILE* inputFile = fopen(argv[1], "r+");
  17.   FILE* tmpFile = fopen("tmp.txt", "w");
  18.   FILE* outputFile = fopen("output.txt", "w+");
  19.  
  20.   char signToChange;
  21.   char readSign;
  22.   char userSign;
  23.  
  24.   printf("Enter sign to change: "); scanf("%c", &signToChange);
  25.   getchar();
  26.   printf("\nEnter sign you want to replace with: "); scanf("%c", &userSign);
  27.    
  28.   while ((readSign = getc(inputFile)) != EOF) {
  29.     if (toupper(readSign) == toupper(signToChange)) {
  30.       fwrite(&userSign, 1, 1, tmpFile);
  31.     }
  32.     else {
  33.       fwrite(&readSign, 1, 1, tmpFile);
  34.     }
  35.  
  36.     if (toupper(readSign) >= 'A' && toupper(readSign) <= 'Z')
  37.       statistic.letter_counter[toupper(readSign) - 'A']++;
  38.     else if (readSign == ' ' || readSign == '\t' || readSign == 'n')
  39.       statistic.white_characters++;
  40.   }
  41.  
  42.   char rarestLetter = 0 + 'A';
  43.   int min = statistic.letter_counter[0];
  44.   for (int i = 0; i < NUM_OF_LETTERS; ++i) {
  45.     if (statistic.letter_counter[i] < min && statistic.letter_counter[i] != 0) {
  46.       min = statistic.letter_counter[i];
  47.       rarestLetter = i + 'A';
  48.     }
  49.   }
  50.   fclose(tmpFile);
  51.   tmpFile = fopen("tmp.txt", "r");
  52.   while ((readSign = getc(tmpFile)) != EOF) {
  53.     if (toupper(readSign) == toupper(rarestLetter)) {
  54.       fprintf(outputFile, "VIRUS");
  55.     }
  56.     else {
  57.       fwrite(&readSign, 1, 1, outputFile);
  58.     }
  59.   }
  60.  
  61.   for (int i = 0; i < NUM_OF_LETTERS; ++i) {
  62.     printf("%c - %d\n", i + 'A', statistic.letter_counter[i]);
  63.   }
  64.  
  65.   fclose(inputFile);
  66.   fclose(tmpFile);
  67.   fclose(outputFile);
  68.   remove("tmp.txt");
  69.   return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement