Home » Ask & Discuss » Board Exams - CBSE, ICSE, State Boards » Computer Science « Back to Discussion



Computer Science

New kid on the Block

Joined: 14 May 2009
Post: 1
14 May 2009 22:43:16 IST
0 People liked this
3
1063 View Post
a program to find the frequency of each word in a string
None

a program to find the frequency of each word in a string


Share this article on:

Comments (3)


Scorching goIITian

Joined: 14 Oct 2007
Posts: 248
15 May 2009 07:35:15 IST
0 people liked this

edited

Scorching goIITian

Joined: 14 Oct 2007
Posts: 248
15 May 2009 07:38:49 IST
0 people liked this

this is the program for finding the frequency of a word try it comment on it and don't forget to rate me cheers

//--- includes
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;

 


//--- prototypes
void countValue(int cnt);
float getAverage();

 

 


//--- constants
const int BINS = 21; // how many numbers can be counted

 

 

 


//--- globals
int valueCount[BINS]; // bins used for counting each number
int totalChars = 0; // total number of characters

// main
int main() {

char c; // input character
int wordLen = 0; // 0 if not in word, else word length

//--- Initialize counts to zero
for (int i=0; i<BINS; i++) {
valueCount[i] = 0;
}

//--- Read chars in loop and decide if in a word or not.
while (cin.get(c)) {
if (isalpha(c)) { // letters are in words, so
wordLen++; // add one to the word length
} else {
countValue(wordLen); // end of word
wordLen = 0; // not in a word, set to zero
}
}
countValue(wordLen); // necessary if word ended in EOF

//--- print the number of words of each length
cout << "Why does this line disappear?" << endl;
cout << "Word length Frequency" << endl;
for (int j=1; j<BINS; j++) {
cout << setw(6) << right << j << " "
<< setw(8) << right << valueCount[j] << endl;
}

//--- print average length
cout << "\nAverage word length: " << getAverage() << endl;

return 0;
}//end main





// countValue
void countValue(int cnt) {
if (cnt > 0) {
// this must be the end of a word
if (cnt > 20) {
cnt = 20; // longer than 20 counts as 20
}
valueCount[cnt]++; // count in correct bin
}
totalChars += cnt;
}//end countWord








// getAverage
float getAverage() {
int totalCount = 0;

for (int i=0; i<BINS; i++) {
totalCount += valueCount[i];
}
if (totalCount > 0) {
return (float)totalChars/totalCount;
} else {
return 0.0;
}
}//end getAverage

Scorching goIITian

Joined: 14 Oct 2007
Posts: 248
15 May 2009 07:42:55 IST
0 people liked this

whats this yaar i can't even type this program here bcoz of this nonsensical table



Quick Reply


Reply

Some HTML allowed.
Keep your comments above the belt or risk having them deleted.
Signup for a avatar to have your pictures show up by your comment
If Members see a thread that violates the Posting Rules, bring it to the attention of the Moderator Team
Free Sign Up!

Preparing for IIT-JEE ?

Arihant Revision Package for IIT JEE - Books, Practice Tests + Rank Predictor


@ INR 1,995/-

For Quick Info

Name

Mobile No.

Find Posts by Topics

Physics.

Topics

Mathematics.

Chemistry.

Biology

Parents

Board

Fun Zone

Sponsored Ads