60 377
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #27137 in C++ for sunday

Question #27137
reads several lines of text and prints a table indicating the number of occurrences of each different word in the text. The first version of your program should include the words in the table in the same order in which they appear in the text.
For example, the lines
Create a function in the StringProcessing class called uniqueWordOccurrence that To be, or not to be: that is the question:
Whether ‘tis nobler in the mind to suffer

Contains the words “to” three times, the word “be” two times, the word “or” once, etc. You should then do a more interesting (and useful) printout in which the words are sorted alphabetically.
Expert's answer
//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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions