Most frequent character

How do make my program find the character from a text file with the most common occurrences and display that character along with the number of occurrences? My program also needs to use the toupper() function to make all lowercase to uppercase.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
   #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main()
    {
      fstream inputFile;
      inputFile.open("letter_count.txt");
      string letter;

         char ch;
         char alphabet[26] = { 0 };
         int counts = 0;
         while (inputFile)
         {
             // Read file
             inputFile.get(ch); 
             if (isalpha(ch))
         {
            //Make all letters upper case    
             toupper(ch);

         //Counts number of occurrences for each letter
         if (ch >= 'A' || ch <= 'Z')
        {
            alphabet[counts++];
        }
    }
}
for (int i = 0; i < 26; i++) //Displays number of occurrences for each         character A-Z
{
    cout << alphabet[counts] << endl;
}

inputFile.close();
system("pause");
return 0;
}
Your alphabet array is supposed to hold the count for each letter; change its type to
int alphabet[26] = { 0 };

ch won't actually be made upper case unless you write
ch = toupper(ch);

The check should use AND (&&) not OR (||):
if (ch >= 'A' && ch <= 'Z')
(although I think the check is redundant anyway at this point).

The counting should increment the count for the array element so many past 'A':
alphabet[ch-'A']++;

If you want to display letter and count then your output should be something like
cout << char('A' + i) << ": " << alphabet[i] << endl;


Variable counts is really redundant.


Nowhere in your code do you find the letter with the largest count. You will need to determine this from the array alphabet[] after you have completed input.


Your indentation is inconsistent and makes your code hard to read.
Last edited on
Change line 15: int alphabet[26] = { 0 };
Change line 24: ch = toupper(ch);
Change line 29: ++(alphabet[ch]);
Change line 35: cout << static_cast<char>('A' + i) << ": " << alphabet[i] << endl;
it is a lot easier to just do
int alphabet[256] = { 0 };
then for any input, you can do
alphabet[somestring[(unsigned char)letterindex]]++;
and lose all the rest of the code.
then if all you care about are a to z, just iterate a to z...
for(char a2z = 'a'; a2z<='z'; a2z++)
cout << alphabet[a2z] << endl; //your counts...

or 2 loops for 'A' to 'Z' and the lower case, or one loop can add 'a' and 'A' ... depending on what you want, but this if fine grained info about your data that you can easily extract a variety of information from depending on what you want. be sure to use unsigned chars when using them as index into the table, is all.

your approach is better for unicode, where making a table of counts is intractable.
then you must map what you want to a smaller table, unless you have a LOT of memory to waste on the effort. So remember what you have if you need to work in unicode, but ascii is so small you can afford to take some cute shortcuts with it.
Last edited on
Topic archived. No new replies allowed.