Advertisement
Guest User

Untitled

a guest
May 30th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5. #define MAX_FILE 1000000
  6. #define WIDTH 80
  7.  
  8. int readFile(char fileName[], char textStr[]);
  9. void printGraph(char a, int count, int max);
  10.  
  11.  
  12. int main(int argc, char* argv[]){
  13.    
  14.     /*setting the max(largest number count) the count holding 26 places (extra for the terminator)
  15.     i to be used in the for loops */
  16.     int  i, max = 0, count[26];
  17.     /*Clearing memory of previous numbers*/
  18.     for(i = 0; i <26; i++) count[i] = 0;
  19.     char textStr[MAX_FILE];
  20.    
  21.     if (argc != 2){
  22.         printf("Invalid number of arguments\n");
  23.         printf("Usage: assign1 file_name\n");
  24.         return 1;
  25.     }
  26.    
  27.     if (readFile(argv[1], textStr) >= MAX_FILE){
  28.         printf("The file is too large\n");
  29.         return 1;
  30.     }
  31.    
  32.     while(textStr[i] != '\0'){
  33.         /*gets the value of the char read from the file and increments the count where the letter read minus the value of
  34.         'a' shows the place in the array where the character is.(letters in array start at 0and end at 25)*/
  35.         if((textStr[i] >='a') && (textStr[i] <= 'z')) count[textStr[i] - 'a']++;
  36.       /*for uppercase*/
  37.       else if((textStr[i] >='A') && (textStr[i]<='Z')) count[textStr[i] - 'A']++;
  38.  
  39.     }
  40.     for(i = 0; i <26; i++){
  41.       if(count[i] > max)
  42.         max = count[i];
  43.     }
  44.     for(i = 0; i <26; i++){
  45.         /*prints out the letter and the count for that letter ex 'a' corrosponds with count[0]*/
  46.      char letter = 'a';
  47.  
  48.       printf(" %c : %i \n", letter + i, count[i]);
  49.     }
  50.     for(i = 0; i <26; i++){
  51.      /*calls the printGraph method to print equal signs for the graph*/
  52.      printGraph('a' + i, count[i],max);
  53.     }
  54.    
  55.     return 0;
  56. }
  57.  
  58. int readFile(char fileName[], char textStr[])
  59. {
  60.     int i = 0;
  61.     FILE *fptr;
  62.    
  63.     if ((fptr=fopen(fileName, "r")) == NULL){
  64.         printf("Error: unknown file %s\n", fileName);
  65.         exit(1);
  66.     }
  67.     return i;  
  68. }
  69. void printGraph(char a, int count, int max)
  70. {
  71.   int i;
  72.   putchar(a);
  73.  
  74.   for(i = 1; i <(WIDTH*count/max); i++) putchar('=');
  75.     putchar('\n');
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement