473,569 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

word freq and length sorting.. need help.. stuck..

2 New Member
hye.. i really need ur guys help.. im stuck.. i want to count the word freq and print them according to the word length.. > the longest be output first..
ouh.. and my input must be alphabet or alhabet+numeric only..

here what i did so far..

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5. class Cmp : public std::binary_function<std::string,std::string,bool> {
  6. public:
  7.   bool operator()(const std::string& s1, const std::string& s2){ //????
  8.   }
  9. };
  10. //typedef std::map<std::string, int, Cmp> MAP;
  11.  
  12. typedef enum {s1, s2} STATUS;
  13. struct Count {
  14.     int N;
  15.     Count() : N(0) {}
  16.     operator int() { return N; }
  17.     void operator++() { N++; }
  18. };
  19. typedef map<string,Count> Map;
  20.  
  21. int main(void){
  22.   char c;
  23.   map<string,Count> m;
  24.   std::string  buffer;
  25.     STATUS status=s1;
  26. //    while( cin >> str ) stringCounts[str]++; 
  27.   while(std::cin >> c){
  28.  
  29.     switch(status){
  30.     case s1:
  31.       if(isalpha(c)){
  32. //    m[string]++;
  33.     buffer=c;
  34.     status=s2;
  35.  
  36.       }
  37.       break;
  38.     case s2:
  39.       if(isalnum(c)){
  40.     buffer+=c;
  41.       }else{
  42.     //std::cout << s1 << s2 << std::endl;
  43.  
  44.     std::cout << c << buffer << std::endl;
  45.     status=s1;
  46.       }
  47.  
  48.       break;
  49.     }
  50.   }
  51.   if(status=s2){
  52.  
  53. Map::iterator i = m.begin();
  54.     while ( i != m.end() ) {
  55.       cout << i->first << ':'
  56.            << i->second << endl;
  57.       ++i;
  58.    std::cout << c << buffer << std::endl;
  59.   }
  60.  
  61.  
  62. }    
  63.   return 0;
  64. }
can you guys please help me..
Jan 10 '07 #1
2 2059
DeMan
1,806 Top Contributor
Just to make sure we're on the same page, do you mean you want to count the frequency of characters (alphabetic and numeral only) in a given input, and output them according to the most frequently occuring, or do you actually want to find the most common word (sequence of alphabetic/numeral characters),

I have assumed the first, and would suggest you make an array of 36 char/int values. Then choose to assign each possible numeral or alpha-character one of these array items. Using a case statement you can populate the array something along the lines of (in pseudocode of course, so I can't be accused of making syntactic mistakes):
Expand|Select|Wrap|Line Numbers
  1. int[] counts = new int[36] to initialise each to 0
  2. while (we have more input)
  3. {
  4.   currentChar = getChar();
  5.   switch (currentChar)
  6.   {
  7.     case '0'
  8.       counts[0]++;
  9.       break;
  10.     case '1': //note the quote marks, we are referring to character not int value
  11.       counts[1]++;
  12.       break;
  13.  /* (same for other numerals) */
  14.  
  15.     case 'a':
  16.     case 'A':
  17.       counts[10]++;
  18.       break;
  19.     case 'b':
  20.     case 'B':
  21.       counts[11]++;
  22. /* Same for other alpha's */
  23.   }
  24. }
  25.  
There are many ways to sort some easier than others, btu I'll leave you with this much so far (in case I got the wrong end of the stick) and you can ask more questions later, which I'm sure someone (possibly me again) can answer if you get stuck....
Jan 10 '07 #2
rehet
2 New Member
hye.. thanksss.. :d

but i finished it already..

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5. class Cmp : public std::binary_function<std::string,std::string,bool> {
  6. public:
  7. bool operator()(const std::string& s1, const std::string& s2){
  8.  
  9. int l1 = s1.length();
  10. int l2 = s2.length();
  11. if(l1==l2){
  12.           return s1>s2;
  13.           }
  14.           else{
  15.                return l1>l2;
  16.                } 
  17.  
  18. }
  19. };
  20. typedef std::map<std::string, int, Cmp> MAP;
  21.  
  22. typedef enum {s1, s2} STATUS;
  23.  
  24. int main(void){
  25.   char c;
  26.   MAP m;
  27.   std::string  buffer;
  28.     STATUS status=s1;
  29.  
  30.   while(std::cin.get(c)){
  31.  
  32.     switch(status){
  33.     case s1:
  34.       if(isalpha(c)){
  35.  
  36.     buffer=c;
  37. //m[buffer]++;
  38.     status=s2;
  39.  
  40.       }
  41.       break;
  42.     case s2:
  43.  
  44.       if(isalnum(c)){
  45.  
  46.     buffer+=c;
  47.       }else{
  48.  
  49. m[buffer]++;
  50.     status=s1;
  51.       }
  52.  
  53.       break;
  54.     }
  55.   }
  56.   if(status=s2){
  57.  
  58.  
  59. m[buffer]++;
  60.   }
  61.  
  62.  
  63.  
  64. for(MAP::iterator i=m.begin(); i!=m.end(); ++i){
  65.  
  66.                   std::cout << i->first << ":" << i->second << std::endl;
  67.  
  68.  
  69. }    
  70.   return 0;
  71. }
  72.  
Jan 11 '07 #3

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

Similar topics

10
3869
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no more than 10 letters, and find all possible mutations that match a word in my dictionary (80k words). However a wildcard letter '?' is also an...
4
10028
by: suzy | last post by:
hello. how can i sort data in a dataset? all the examples i have seen on msdn, etc are sorting a dataview. this works fine, but i want to return the results in xml and the dataview doesn't have a .getxml method (unlike the dataset). any ideas? thanks.
8
3498
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word...
8
7507
by: Frost | last post by:
Hi All, I am a newbie i have written a c program on unix for line by line comparison for two files now could some one help on how i could do word by word comparison in case both lines have the same words but in jumbled order they should match and print only the dissimilar lines.The program also checks for multiple entries of the same line....
1
4205
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD: (there is a WORD in ( my string WORD )) and * WORD * to (find WORD) and * WORD * Should give me the to word between star (star ar not part of string)
8
4078
by: Andrew Savige | last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of names: names = "freddy fred bill jock kevin andrew kevin kevin jock" ...
0
1903
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and PreviousToken(Previous word) and frequency and probability From text file and put each one in cell in table For example if the text file content is "Every man has...
5
2530
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and PreviousToken(Previous word)) from multube files and get frequency of both. it can get for single word but double word give error line 50, in...
11
6775
by: arnuld | last post by:
C takes input character by character. I did not find any Standard Library function that can take a word as input. So I want to write one of my own to be used with "Self Referential Structures" of section 6.5 of K&R2. K&R2 has their own version of <getwordwhich, I think, is quite different from what I need: <getwordwill have following...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.