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.. - #include <iostream>
-
#include <string>
-
#include <map>
-
using namespace std;
-
class Cmp : public std::binary_function<std::string,std::string,bool> {
-
public:
-
bool operator()(const std::string& s1, const std::string& s2){ //????
-
}
-
};
-
//typedef std::map<std::string, int, Cmp> MAP;
-
-
typedef enum {s1, s2} STATUS;
-
struct Count {
-
int N;
-
Count() : N(0) {}
-
operator int() { return N; }
-
void operator++() { N++; }
-
};
-
typedef map<string,Count> Map;
-
-
int main(void){
-
char c;
-
map<string,Count> m;
-
std::string buffer;
-
STATUS status=s1;
-
// while( cin >> str ) stringCounts[str]++;
-
while(std::cin >> c){
-
-
switch(status){
-
case s1:
-
if(isalpha(c)){
-
// m[string]++;
-
buffer=c;
-
status=s2;
-
-
}
-
break;
-
case s2:
-
if(isalnum(c)){
-
buffer+=c;
-
}else{
-
//std::cout << s1 << s2 << std::endl;
-
-
std::cout << c << buffer << std::endl;
-
status=s1;
-
}
-
-
break;
-
}
-
}
-
if(status=s2){
-
-
Map::iterator i = m.begin();
-
while ( i != m.end() ) {
-
cout << i->first << ':'
-
<< i->second << endl;
-
++i;
-
std::cout << c << buffer << std::endl;
-
}
-
-
-
}
-
return 0;
-
}
can you guys please help me..
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): -
int[] counts = new int[36] to initialise each to 0
-
while (we have more input)
-
{
-
currentChar = getChar();
-
switch (currentChar)
-
{
-
case '0'
-
counts[0]++;
-
break;
-
case '1': //note the quote marks, we are referring to character not int value
-
counts[1]++;
-
break;
-
/* (same for other numerals) */
-
-
case 'a':
-
case 'A':
-
counts[10]++;
-
break;
-
case 'b':
-
case 'B':
-
counts[11]++;
-
/* Same for other alpha's */
-
}
-
}
-
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....
hye.. thanksss.. :d
but i finished it already.. -
#include <iostream>
-
#include <string>
-
#include <map>
-
using namespace std;
-
class Cmp : public std::binary_function<std::string,std::string,bool> {
-
public:
-
bool operator()(const std::string& s1, const std::string& s2){
-
-
int l1 = s1.length();
-
int l2 = s2.length();
-
if(l1==l2){
-
return s1>s2;
-
}
-
else{
-
return l1>l2;
-
}
-
-
}
-
};
-
typedef std::map<std::string, int, Cmp> MAP;
-
-
typedef enum {s1, s2} STATUS;
-
-
int main(void){
-
char c;
-
MAP m;
-
std::string buffer;
-
STATUS status=s1;
-
-
while(std::cin.get(c)){
-
-
switch(status){
-
case s1:
-
if(isalpha(c)){
-
-
buffer=c;
-
//m[buffer]++;
-
status=s2;
-
-
}
-
break;
-
case s2:
-
-
if(isalnum(c)){
-
-
buffer+=c;
-
}else{
-
-
m[buffer]++;
-
status=s1;
-
}
-
-
break;
-
}
-
}
-
if(status=s2){
-
-
-
m[buffer]++;
-
}
-
-
-
-
for(MAP::iterator i=m.begin(); i!=m.end(); ++i){
-
-
std::cout << i->first << ":" << i->second << std::endl;
-
-
-
}
-
return 0;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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...
|
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....
|
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)
| |
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"
...
|
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...
|
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...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |