Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define NUM_OF_LETTERS 'Z' - 'A'
- struct Stats {
- int white_characters;
- int letter_counter[NUM_OF_LETTERS];
- };
- struct Stats statistic;
- int main(int argc, char* argv[]) {
- FILE* inputFile = fopen(argv[1], "r+");
- FILE* tmpFile = fopen("tmp.txt", "w");
- FILE* outputFile = fopen("output.txt", "w+");
- char signToChange;
- char readSign;
- char userSign;
- printf("Enter sign to change: "); scanf("%c", &signToChange);
- getchar();
- printf("\nEnter sign you want to replace with: "); scanf("%c", &userSign);
- while ((readSign = getc(inputFile)) != EOF) {
- if (toupper(readSign) == toupper(signToChange)) {
- fwrite(&userSign, 1, 1, tmpFile);
- }
- else {
- fwrite(&readSign, 1, 1, tmpFile);
- }
- if (toupper(readSign) >= 'A' && toupper(readSign) <= 'Z')
- statistic.letter_counter[toupper(readSign) - 'A']++;
- else if (readSign == ' ' || readSign == '\t' || readSign == 'n')
- statistic.white_characters++;
- }
- char rarestLetter = 0 + 'A';
- int min = statistic.letter_counter[0];
- for (int i = 0; i < NUM_OF_LETTERS; ++i) {
- if (statistic.letter_counter[i] < min && statistic.letter_counter[i] != 0) {
- min = statistic.letter_counter[i];
- rarestLetter = i + 'A';
- }
- }
- fclose(tmpFile);
- tmpFile = fopen("tmp.txt", "r");
- while ((readSign = getc(tmpFile)) != EOF) {
- if (toupper(readSign) == toupper(rarestLetter)) {
- fprintf(outputFile, "VIRUS");
- }
- else {
- fwrite(&readSign, 1, 1, outputFile);
- }
- }
- for (int i = 0; i < NUM_OF_LETTERS; ++i) {
- printf("%c - %d\n", i + 'A', statistic.letter_counter[i]);
- }
- fclose(inputFile);
- fclose(tmpFile);
- fclose(outputFile);
- remove("tmp.txt");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement