count the number of letter occurrence in a file

how to do this? write a c++ program that given the name of a text file reads that file and counts the number occurrences of each alphabetic letter in the text. treat upper and lower case instances of a letter as the same letter. hint: look up tolower()---testing the case isn't necessary. if ch is a char, then ch-'a' is a valid c++ expression and will evaluate to 0 when ch is equal to 'a'---try 'b', 'c', and 'd'!)..
really need your help..this is for my case study.
You can try the hash table.It's a O(n) algorithm, n is the length of text.
this is the program that I made.

#include<iostream.h>
#include<ctype.h>
#include<fstream.h>


#define NUM_LETTERS 26



// I have ch there to keep track of count i.e. which char has what count.

//check the last part of main where I am printing, you will get it.

struct Letter_Stats

{

int count;

char ch;

};



int main()

{

ifstream inputfile;

char ch;

int i;

Letter_Stats stats[NUM_LETTERS];

inputfile.open("108.txt", ios::in);

if (inputfile.fail())

{

cout << "error - bailing out";

return 1;

}

//initialize the array of structures

for (i=0;i<NUM_LETTERS;i++)

{

stats[i].count=0;

stats[i].ch=('a' + i);

}

//do till file ends

while (!inputfile.eof())

{
inputfile.get(ch);

//increment counter for appropriate character



if('a' <= ch && ch <= 'z')

stats[ch - 'a'].count++;

else if('A' <= ch && ch <= 'Z')

stats[ch - 'A'].count++;
}

inputfile.close();

//print characters and count if it is more than 0

for(i=0;i<NUM_LETTERS;i++)

{

if(stats[i].count>0)

{

cout<<stats[i].ch<<" occured "<<stats[i].count<<" times"<<endl;

}

}



return 0;

}
but it doesn't count the lower and uppercase letter as the one. my teacher says that I should use a tolower().
//increment counter for appropriate character



if('a' <= ch && ch <= 'z')

stats[ch - 'a'].count++;

else if('A' <= ch && ch <= 'Z')

stats[ch - 'A'].count++;
}

I haven't looked it up nor tried it, but it seems to me with using tolower() you could shorten this to the following:

if ('a' <= tolower(ch) && tolower(ch) <= 'z')

stats[ch - 'a'].count++;
}

tolower(ch) gives the lowercase equivalent of the character if it is uppercase, and returns the input character if it is lowercase. You can look the method up in the C++ reference: http://www.cplusplus.com/reference/clibrary/cctype/tolower/
i tried what you told me Sam but it doesn't output how many letters are there in the text file.. my program goes like this, I will create a text file "108.txt" and I will input letters in it like AabcdE..so when I run the program the output would be 5.it won't output 6 bcoz the program reads 'A' 'a' as one. so instead of 6 letters it is only 5.
works fine for me

output is

a occured 2 times
b occured 1 times
c occured 1 times
d occured 1 times
e occured 1 times
how do I count all the letters as one?
instead of
a occured 2 times
b occured 1 times
c occured 1 times
d occured 1 times
e occured 1 times
the output would be like
there are 6 letters in the text file.
I did it! thank you all for your help. it really helped me.. :)
Topic archived. No new replies allowed.