Connecting Tech Pros Worldwide Forums | Help | Site Map

outputing words in a string and number of times they occur

Newbie
 
Join Date: Dec 2006
Posts: 1
#1: Dec 6 '06
Hi. i need some help with some homework i was given. i am using windows visual studio to make my program. the program should read in a string of characters then output the the words in that string and how many times it occurs. any help would be appreciated

Newbie
 
Join Date: Dec 2006
Posts: 1
#2: Dec 6 '06

re: outputing words in a string and number of times they occur


I have a similiar program, it ask you to enter a string, then enter a character, then it displays the number of times the character occurs in the string. I'm using c++ programming.

any help would be useful.
DeMan's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#3: Dec 6 '06

re: outputing words in a string and number of times they occur


This question comes up remarkably often, and you should look through the posts before submitting. You should also (in any forum) post any code that can show your progress (you will find people reluctant to do your entire assignment for you).

Basically what I would do is:
1)get a String (and array/stream of character) from the input.
2)sort the string into alphabetical (or any order so long as like characters appear together) order.
3) when the search character is entered, look for it (by iterating the entire length or by other means) in the string and count for how long it occurs .

eg if
Input = "The rain in spain falls mainly in the plain"
sortedString = "aaaaaefhiiiiiillllmnnnnnnpprst"
char=i;
loop characters to find i then count
Expand|Select|Wrap|Line Numbers
  1. while(currentChar==i)
  2. {
  3.   counter++;
  4.   *currentChar++
  5. }
  6.  
This is not necessarily the best (or even easiest answer) but I leave it tou you to either implement, improve on or find something else....
Reply