By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

counting the percentage and occurence of each identical word in input file

P: 6
I am an intermediate programmer and i tried to solve the following programme but with no success the programme is a simple text analyzer that read an input file and cumpute the occurence and percentage of each similar word .for example lets consider the following input file.

"The for structure totals the elements of the array one row at the time. The outer for structure begins by setting row of the array to 0."

the analysis of the programme shold be the following:

Word Frequency %
the 6 22.25%
for 2 7.41%
structure 2 7.41%
totals 1 3.70%
elements 1 3.70%
of 2 7.41%
Array 2 7.41%
one 1 3.70%
row 2 7.41%
at 1 3.70%
time 1 3.70%
outer 1 3.70%
begins 1 3.70%
by 1 3.70%
setting 1 3.70%
to 1 3.70 %
0 1 3.70%

Total words 27
Sep 23 '06 #1
Share this Question
Share on Google+
4 Replies


P: 6
I am an intermediate programmer and i tried to solve the following programme but with no success the programme is a simple text analyzer that read an input file and cumpute the occurence and percentage of each similar word .for example lets consider the following input file.

"The for structure totals the elements of the array one row at the time. The outer for structure begins by setting row of the array to 0."

the analysis of the programme shold be the following:

Word///////////////// Frequency ////////////// %
the///////////// 6 ///////////// 22.25%
for//////////// 2 //////////// 7.41%
structure///////// 2 /////////// 7.41%
totals /////////// 1 ////////// 3.70%
elements /////// 1 ///////// 3.70%
of ////// 2 //////// 7.41%
Array//////// 2 ////// 7.41%
one//////// 1 //////// 3.70%
row/////////// 2 //////// 7.41%
at/////////// 1 ////// 3.70%
time/////////// 1 ///// 3.70%
outer ////////// 1 //// 3.70%
begins /////// 1 /// 3.70%
by ////// 1 // 3.70%
setting ///// 1 ///// 3.70%
to ///// 1 ///// 3.70 %
0 //// 1 //// 3.70%

Total words////////////////////////// 27
Sep 23 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
Have a go yourself, then we'll help iron out the glitches.

Expand|Select|Wrap|Line Numbers
  1. open read 
  2. do
  3.   read file line
  4.   issolate words on the line (using pointers or strtok)
  5.   for each word
  6.     if this is the first time the word has appeared
  7.       add word to list of found words with a recurance count of 1
  8.     else
  9.       find word in list of words and increment it's recurance count
  10.     endif
  11.   endfor
  12. while(you haven't reached the end of the file)
  13.  
  14. Generate output
  15.  
Sep 23 '06 #3

P: 6
can you tell me how to copy the text from an array to a pointer to acharacter because i have use infile.getline and it does not accept pointers to charachter parameters so,i have used array.

thank you,
Sep 23 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
I think you are a little confused. The would be no need to copy to or even have a pointer to char to hold your data. Your array is all you need so

Expand|Select|Wrap|Line Numbers
  1. char line[250];
  2. char *pSpace;
  3. ifstream infile("<filename>");
  4.  
  5. infile.getline(line, sizeof line);
  6.  
  7. pSpace = strchr(line, ' ');
  8.  
In this code line is an array of char. However just using line by itself in the code provides a pointer to that array. line has type char *, but it is not a pointer.

I have a pointer in my code to hold the location of the first space in the line.
Sep 23 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.