How do I find out the most common char and how often it occurs in a textfile?
////////////////////////////////////////////////////////////////////////////////////
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int main()
{
char c;
int up_ct = 0;
int low_ct = 0;
int dig_ct = 0;
int n_words = 0;
int punc_ct = 0;
int rows = 1;
int total = 0;
bool inword = false;
FILE *fp;
fp = fopen("e:\\test.txt", "r");
while ((c = getc(fp)) != EOF)
{
if (islower(c))
low_ct++;
else if (isupper(c))
up_ct++;
else if (isdigit(c))
dig_ct++;
else if (ispunct(c))
punc_ct++;
else if ('\n' == c)
rows++;
if (!isspace(c) && !inword)
{
inword = true;
n_words++;
}
if (isspace(c) && inword)
inword = false;
}
total = up_ct + low_ct + dig_ct;
printf("\nwords = %d, totalchar = %d, lowercase = %d, uppercase = %d "
"digits = %d, punctuation = %d, rows = %d",
n_words, total, low_ct, up_ct, dig_ct, punc_ct, rows);
fclose(fp);
return 0;
}