2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

want to count upper case 'A' and lower case 'a' separately or as a single character?

I would create an int array of 256 to represent all the possible characters in a text file. Actually that's more than is possible in a text file, but it makes the math easier. Then as you read each character you can let the character itself index into that array. For example, if the program reads the letter 'A'

int arry[256] = {0}; // declare the array

<snip>
int c;
// read a single character
infile.get(c);
// increment the counter for that character
arry[c]++;

Put the above in a loop, when the reading is all done you have the sum for each letter. Just loop through that array and print the values that are greater than 0

for(int i = 0; i < 256; i++)
{
    if( arry[i] > 0)
        cout << "Letter " << (char)i << " = " << arry[i] << "\n";
 }

You could also probably do this with <map> but the coding would be a little more difficult.

Votes + Comments
definately a guru
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.