//Not sorted
#include <istream>
#include <ostream>
#include <map>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
#include <cctype>
class StringProcessing {
public:
void uniqueWordOccurence(istream &inf, ostream &outf) {
& vector<pair <string, unsigned> > occur;
& while (!inf.eof()) {
string line;
getline(inf, line);
unsigned pos = 0;
string word;
while (getNextWord(line, &pos, &word)) {
transform(word.begin(), word.end(), word.begin(), ::tolower);
//occur[word]++;
bool contains = false;
for (unsigned i = 0; i < occur.size() && !contains; i++)
& if (occur[i].first == word) {
++occur[i].second;
contains = true;
& }
if (!contains) occur.push_back(make_pair(word, 1));
}
& }
& for (unsigned i = 0; i < occur.size(); i++)
outf << occur[i].first << "& " << occur[i].second << "\n";
}
private:
bool getNextWord(const string &text, unsigned *pos, string *word) {
& *word = "";
& while (*pos < text.length() &&
& !isalpha(text[*pos]) && text[*pos] != '‘') {
(*pos)++;
& };
& if (*pos >= text.length()) return false;
& char c = text[*pos];
& while (*pos < text.length() &&
& (isalpha(c) || c == '-' || c == '‘')) {
*word += text[(*pos)++];
if (*pos < text.length()) c = text[*pos];
& }
& if (*pos >= text.length() || string("!., :?()").find(c) != string::npos)
return true;
& return false;
}
};
int main() {
StringProcessing str_proc;
ifstream inf("input.txt");
ofstream outf("output.txt");
str_proc.uniqueWordOccurence(inf, outf);
}
// Sorted
#include <istream>
#include <ostream>
#include <map>
#include <fstream>
#include <algorithm>
using namespace std;
#include <cctype>
class StringProcessing {
public:
void uniqueWordOccurence(istream &inf, ostream &outf) {
& //string text = "";
& map<string, unsigned> occur;
& while (!inf.eof()) {
string line;
getline(inf, line);
unsigned pos = 0;
string word;
while (getNextWord(line, &pos, &word)) {
transform(word.begin(), word.end(), word.begin(), ::tolower);
occur[word]++;
}
& }
& for (map<string, unsigned>::iterator it = occur.begin();
& it != occur.end(); ++it)
outf << it->first << "& " << it->second << "\n";
}
private:
bool getNextWord(const string &text, unsigned *pos, string *word) {
& *word = "";
& while (*pos < text.length() &&
& !isalpha(text[*pos]) && text[*pos] != '‘') {
(*pos)++;
& };
& if (*pos >= text.length()) return false;
& char c = text[*pos];
& while (*pos < text.length() &&
& (isalpha(c) || c == '-' || c == '‘')) {
*word += text[(*pos)++];
if (*pos < text.length()) c = text[*pos];
& }
& if (*pos >= text.length() || string("!., :?()").find(c) != string::npos)
return true;
& return false;
}
};
int main() {
StringProcessing str_proc;
ifstream inf("input.txt");
ofstream outf("output.txt");
str_proc.uniqueWordOccurence(inf, outf);
}
Comments
Leave a comment