In: Computer Science
C++
Add a function that finds & displays all the words that begin with the letter 's'.
The text file "dickens.txt" is as follows:
It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair...
Display this at the end of the program which should look like this:
WORDS THAT BEGIN WITH 'S': season, spring
"spring" appears twice, duplicate works should only appear once.
The code is as follows:
#include <iostream>
#include <fstream>
#include <string>
#include <set>
using namespace std;
multiset<string> display_and_load_words(string
filename);
set<string> get_unique_words(multiset<string>&
words);
int main() {
cout << "The Word Counter program\n\n";
string filename = "dickens.txt";
cout << "FILE TEXT: ";
auto words = display_and_load_words(filename);
cout << "WORD COUNT: " << words.size()
<< endl << endl;
auto unique_words = get_unique_words(words);
cout << unique_words.size() << " UNIQUE WORDS:
";
for (string word : unique_words) {
cout << word << '
';
}
cout << endl << endl;
cout << "COUNT PER WORD: ";
for (string word : unique_words) {
cout << word << '='
<< words.count(word) << ' ';
}
cout << endl << endl;
}
multiset<string> display_and_load_words(string filename)
{
multiset<string> words;
ifstream infile(filename);
if (infile) {
string word;
while (infile >> word) {
cout <<
word << ' ';
string new_word = "";
for (char c : word) {
if (c == '.' || c == ',') {
continue;
}
else if (isupper(c)) {
new_word += tolower(c);
}
else {
new_word += c;
}
}
words.insert(new_word);
}
cout << endl <<
endl;
infile.close();
}
return words;
}
set<string> get_unique_words(multiset<string>&
words) {
set<string> unique_words;
for (string word : words) {
auto search =
unique_words.find(word);
if (search == unique_words.end())
{
unique_words.insert(word);
}
}
return unique_words;
}
Find the code below with new functions implemented,
findsMatchingWords() and printMatchingWords() :
#include <iostream>
#include <fstream>
#include <string>
#include <set>
using namespace std;
multiset<string> display_and_load_words(string filename);
set<string> get_unique_words(multiset<string> &words);
int main(){
cout << "The Word Counter program\n\n";
string filename = "dickens.txt";
cout << "FILE TEXT: ";
auto words = display_and_load_words(filename);
cout << "WORD COUNT: " << words.size() << endl<< endl;
auto unique_words = get_unique_words(words);
cout << unique_words.size() << " UNIQUE WORDS: ";
for (string word : unique_words){
cout << word << ' ';
}
cout << endl<< endl;
cout << "COUNT PER WORD: ";
for (string word : unique_words){
cout << word << '=' << words.count(word) << ' ';
}
cout << endl << endl;
// Added New function that finds & displays all the words that begin with the letter 's'.
set<string> matching_words = findsMatchingWords(words);
printMatchingWords(matching_words);
}
multiset<string> display_and_load_words(string filename){
multiset<string> words;
ifstream infile(filename);
if (infile){
string word;
while (infile >> word){
cout << word << ' ';
string new_word = "";
for (char c : word){
if (c == '.' || c == ','){
continue;
}
else if (isupper(c)){
new_word += tolower(c);
}
else{
new_word += c;
}
}
words.insert(new_word);
}
cout << endl << endl;
infile.close();
}
return words;
}
set<string> get_unique_words(multiset<string> &words){
set<string> unique_words;
for (string word : words){
auto search = unique_words.find(word);
if (search == unique_words.end()){
unique_words.insert(word);
}
}
return unique_words;
}
set<string> findsMatchingWords(multiset<string> &words){
set<string> matching_words;
for (string word : words){
if(word[0] == 'S' || word[0] == 's'){
matching_words.insert(word);
}
}
return matching_words;
}
void printMatchingWords(set<string> &matching_words){
for (auto it=matching_words.begin(); it != matching_words.end(); ++it)
cout << *it<<endl;
}
If you have any doubts please put in the comments. Also, do upvote the solution.