Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <math.h>
- #define MAX_FILE 1000000
- #define WIDTH 80
- int readFile(char fileName[], char textStr[]);
- void printGraph(char a, int count, int max);
- int main(int argc, char* argv[]){
- /*setting the max(largest number count) the count holding 26 places (extra for the terminator)
- i to be used in the for loops */
- int i, max = 0, count[26];
- /*Clearing memory of previous numbers*/
- for(i = 0; i <26; i++) count[i] = 0;
- char textStr[MAX_FILE];
- if (argc != 2){
- printf("Invalid number of arguments\n");
- printf("Usage: assign1 file_name\n");
- return 1;
- }
- if (readFile(argv[1], textStr) >= MAX_FILE){
- printf("The file is too large\n");
- return 1;
- }
- while(textStr[i] != '\0'){
- /*gets the value of the char read from the file and increments the count where the letter read minus the value of
- 'a' shows the place in the array where the character is.(letters in array start at 0and end at 25)*/
- if((textStr[i] >='a') && (textStr[i] <= 'z')) count[textStr[i] - 'a']++;
- /*for uppercase*/
- else if((textStr[i] >='A') && (textStr[i]<='Z')) count[textStr[i] - 'A']++;
- }
- for(i = 0; i <26; i++){
- if(count[i] > max)
- max = count[i];
- }
- for(i = 0; i <26; i++){
- /*prints out the letter and the count for that letter ex 'a' corrosponds with count[0]*/
- char letter = 'a';
- printf(" %c : %i \n", letter + i, count[i]);
- }
- for(i = 0; i <26; i++){
- /*calls the printGraph method to print equal signs for the graph*/
- printGraph('a' + i, count[i],max);
- }
- return 0;
- }
- int readFile(char fileName[], char textStr[])
- {
- int i = 0;
- FILE *fptr;
- if ((fptr=fopen(fileName, "r")) == NULL){
- printf("Error: unknown file %s\n", fileName);
- exit(1);
- }
- return i;
- }
- void printGraph(char a, int count, int max)
- {
- int i;
- putchar(a);
- for(i = 1; i <(WIDTH*count/max); i++) putchar('=');
- putchar('\n');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement