pages:
- 1
hello everyone i use
while(getline(cin,s)){
if(s == "\0") break;
for end of file but is not correct :(((
AMiR SAYS
It's hard to tell what's wrong without looking at your code.
You can take a look at my code.
#include <string>
#include <map>
#include <iostream>
#include <ctype.h>
#include <sstream>
using namespace std;
stringstream in;
void c(string& s) {
string p=",.;'\"()/:-";
for (int i=0;i<s.length();i++) {
if (p.find(s[i]) == string::npos) in << (char)tolower(s[i]);
else in << " ";
}
in << " ";
}
int main () {
map<string,int> m;
string s;
int mx = 0;
while (cin >> s) c(s);
while (in >> s) {
if (m.find(s) == m.end()) m[s]=0;
m[s]++;
mx = max(mx, m[s]);
}
cout << mx << " occurrences" << endl;
for (map<string,int>::iterator it=m.begin();it!=m.end();it++) if (it->second == mx) cout << it->first << endl;
return 0;
}
#include <iostream>
#include <map>
#include <iomanip>
#include <sstream>
using namespace std;
template <typename T> string to_str(T& c){
ostringstream s;
s << c;
return s.str();
}
string ff(string f){
string r;
for(int i=0;i<f.length();i++){
char c = tolower(*(f.substr(i,1).c_str()));
if (c == ',' || c == '.' || c == ':' || c == ';'
|| c == '"'|| c == '\'' || c == '('|| c == ')'
|| c == '"'|| c == '\\' || c == '/'|| c == '-')
{
}
else
r.append(to_str(c));
}
return r;
}
int main()
{
string s;
map<string, int> m;
int mx=0;
while(getline(cin,s)){
if(s == "\0") break;
while(s.length()>0){
int i;
string f;
if((i=s.find(" "))>=0){
f = ff(s.substr(0,i));
s = s.substr(i+1);
}else
{
f = ff(s);
s.clear();
}
map<string, int>::iterator it = m.find(f);
if(it != m.end()){
m[f] += 1;
if (mx < m[f])
mx = m[f];
}
else{
m[f] = 1;
if (mx==0) mx=1;
}
}
}
cout << mx << " occurrences" << endl;
for (std::map<string,int>::iterator it=m.begin(); it!=m.end(); ++it)
if(it->second == mx)
cout << it->first << endl;
return 0;
}
Login please!
In order to post something you must login first. You can use the form in the top of this page.