Hi there
Ok, basically I'm pretty new to C and have created a mashed-together program using different elements from different existing programs:
As you can probably tell, I'm trying to prompt the user for a file name then conduct a letter frequency analysis on the text contained in said file.Code:#include <stdio.h> #define ALPHABET_TOTAL 26 /* Defines the total number of letters in the alphabet */ main() { int text[ALPHABET_TOTAL] = {0}, /* Creates an array of size 26, acting as counters initialized as 0*/ text_input, /* Reads in the text input as characters */ ASCII_count, /* ASCII number of the letters in the alphabet */ alpha; /* boolean flag which turns to 0 (false) in case of a non-alpha character */ /* Setting values to 0 */ text_input = 0; ASCII_count = 0; alpha = 0; /* Get input - this needs to change!! */ FILE *userfile; /* declares file pointer */ printf("Enter file name to load: "); scanf("%s", filename); userfile = fopen("%s", "r"); /* Perform text analysis*/ while( text_input != EOF ) { alpha = 1; text_input = getchar(); /* If input is alphabetical, convert to a number 0-25 so that it can be used as a locator along the array */ if(text_input >= 'a' && text_input <= 'z') text_input -= 'a'; else if(text_input >= 'A' && text_input <= 'Z') text_input -= 'A'; /* Non-alphabetical character, set the alpha flag to false */ else alpha = 0; /* Increment the appropriate counter in the text array as long as the character is alphabetical */ if(alpha) text[text_input]++; } /* Display results */ for (ASCII_count; ASCII_count <= 25; ASCII_count++) printf("\nTotal %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A',text[ASCII_count]); }
What's wrong with what I've got? I'm guessing it's the code prompting for the user file.
Also, If I wanted to add in a counter to keep track of the total number of characters in the text, whereabouts in the various loops would it go?
Any help would be greatly appreciated. I really want to get to grips with C!