Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,081 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,589 people online right now. Registration is fast and FREE... Join Now!




Letter Frequencies in a Text File

 
Reply to this topicStart new topic

Letter Frequencies in a Text File, Here is the source code of a program that counts the number of capital

Entropicvamp
20 Apr, 2008 - 08:56 PM
Post #1

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 14


My Contributions
This is what I have so far but, the problem is I have to to modify this to use comparison operators with characters, and to convert characters to their ASCII integer equivalents. So both capital and under case letters are counted and the count of each individual letter is displayed in a table. Anyone interested in helping me out with this, it would be much appreciated.

CODE
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char FileName[] = "c:\\TestCount.txt";

int main () {
string lineBuffer;
ifstream inMyStream (FileName); //open my file stream

if (inMyStream.is_open()) {
       //create an array to hold the letter counts
       int upperCaseCount[26] = {0};

       //read the text file
       while (!inMyStream.eof() ){

              //get a line of text
              getline (inMyStream, lineBuffer);

             //read through each letter in the lineBuffer
             char oneLetter;
             for( int n=0; n < lineBuffer.length(); ++n ){
                    oneLetter = char( lineBuffer[n] ); //get a letter
                    if (oneLetter >= 'A' && oneLetter <='Z') { //decide if it is a capital letter
                          upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                    }
            }
        }

        inMyStream.close(); //close the file stream

        //display the counts
        for (int i= 0; i < 26; i++)
                cout << char(i + 65) << "\t\t" << upperCaseCount[i] << endl;
        }
        else cout << "File Error: Open Failed";

       return 0;
}



User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Letter Frequencies In A Text File
21 Apr, 2008 - 12:17 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
I think you imagine this is more difficult than it is. I just did this program myself (though I did it in Perl where it is only about 3 lines long). The trick is something called an associative array. C++ calls these maps.

all that you really need is to include the cctypes header which has the nice isalpha() function which you can use to tell if this is an alphabetical character. Then you just the map to keep track of the frequency of each letter.

so you need something like this (note this code is not compiled and more than likey has some typos):
CODE

#include <iostream>
#include<map>
#include <cctype>
#include <string>

using namespace std;

int main() {
map<char, int> chars;
string input;
cout << "Enter a string to count: ";
cin >> input;
for (int i = 0; i < input.length(); i++) {
    if (isalpha(input[i])) {
        chars[input[i]]++; //add one
    }
}
//do something with the data here...

return 0;
}



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:40PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month