Ok, another one
Trying to print the 'average word length' which I'm trying to define as total characters/total words
Do all of these variables need to be defined as doubles from the offset?
Code:
#include <stdio.h>
#include <string.h>
#define ALPHABET_TOTAL 26
int main()
{
/*defining variables*/
FILE *fp;
int text[ALPHABET_TOTAL] = {0},
text_input,
ASCII_count;
double letter_count,
word_count,
word_average;
char filename[20];
/*initializing*/
text_input = 0;
ASCII_count = 0;
letter_count = 0;
word_count = 1;
word_average = 0;
/*user prompt*/
printf("Type a file name, followed by the <enter> key : ");
if(fgets(filename,sizeof(filename),stdin))
{
char* p = strchr(filename, '\n');
if(p) *p = '\0';
}
else
{
puts("Failed to read a file name");
return 1;
}
fp=fopen(filename,"r");
/*text analysis*/
while( (text_input = fgetc(fp)) != EOF )
{
if(text_input >= 'a' && text_input <= 'z')
text_input -= 'a',
letter_count++;
else if(text_input >= 'A' && text_input <= 'Z')
text_input -= 'A',
letter_count++;
else if(text_input == ' ')
word_count++;
else
continue;
text[text_input]++;
}
word_average = letter_count/word_count;
/*display results*/
printf(" Average word length: %d ", word_average);
for (ASCII_count = 0; ASCII_count <= 25; ASCII_count++)
printf("\n Total %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A',text[ASCII_count]);
return 0;
}