Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <Windows.h>
  4.  
  5. int countOccurence(char symbol, char* arr, int arrSize);
  6. int getCharCode(char ch, char* arrWithCodes, int arrSize);
  7.  
  8. int main()
  9. {
  10.     FILE* file = NULL;
  11.     char filename[100];
  12.     printf("Enter filename: ");
  13.     scanf("%s", &filename);
  14.  
  15.     file = fopen(filename, "r");
  16.     if (file == NULL) {
  17.         printf("Error, while opening file: %s", filename);
  18.         system("Pause");
  19.         return -1;
  20.     }
  21.    
  22.     char ch;
  23.     int chCounter = 0;
  24.    
  25.     // map
  26.     char symbols[95];
  27.     int symbols_occurs[95];
  28.     int symbols_dec[95];
  29.  
  30.     for (int i = 0; i < 94; i++) {
  31.         symbols[i] = (char)(i + 32);
  32.         symbols_occurs[i] = 0;
  33.         symbols_dec[i] = i + 32;
  34.     }
  35.  
  36.  
  37.     while ( (ch = fgetc(file)) != EOF) {
  38.         chCounter++;
  39.         int curr = getCharCode(ch, symbols, 95);
  40.         symbols_occurs[curr] += 1;
  41.     }
  42.  
  43.     // Prints statistic
  44.     printf("Symbol => ASCII-DEC => ASCII-HEX => Count: \n");
  45.     for (int i = 0; i < 94; i++) {
  46.         if (symbols_occurs[i] != 0) {
  47.             printf("%c => %d => %x => %d \n", symbols[i], symbols_dec[i], symbols[i], symbols_occurs[i]);
  48.         }
  49.     }
  50.  
  51.     fclose(file);
  52.  
  53.     char srchCh;
  54.     printf("Type character, to search for: ");
  55.     scanf(" %c", &srchCh);
  56.  
  57.     int srchChCode = getCharCode(srchCh, symbols, 95);
  58.     int srchChOccurs = symbols_occurs[srchChCode];
  59.  
  60.  
  61.     printf("Frequence: %f ", (((double)srchChOccurs / (double)chCounter)) );
  62.  
  63.     char chToBeSWP;
  64.     char chToSWP;
  65.     printf("Which character you want to swap and with what? ");
  66.     scanf(" %c %c", &chToBeSWP, &chToSWP);
  67.  
  68.     FILE* outputFile = fopen("output.txt", "w");
  69.     while ()
  70.    
  71.     system("Pause");
  72.     return 0;
  73. }
  74.  
  75. int countOccurence(char symbol, char* arr, int arrSize) {
  76.     int result = 0;
  77.  
  78.     for (int i = 0; i < arrSize; i++) {
  79.         if (arr[i] == symbol) {
  80.             result++;
  81.         }
  82.     }
  83.  
  84.     return result;
  85. }
  86.  
  87. int getCharCode(char ch, char* arrWithCodes, int arrSize) {
  88.     for (int i = 0; i < arrSize; i++) {
  89.         if (arrWithCodes[i] == ch) {
  90.             return i;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement