Question

In: Computer Science

C++ Add a function that finds & displays all the words that begin with the letter...

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;
}

Solutions

Expert Solution

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.


Related Solutions

Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
An alliteration is a stylistic device in which successive words begin with the same letter (or...
An alliteration is a stylistic device in which successive words begin with the same letter (or sometimes the same sound). In our case we will only consider alliterations that follow the "same letter" property. It is often the case that short words (three letters or less) are inserted to make the sentence make sense. Here are some examples of alliterations: alliteration's artful aid Peter parker's poems are pretty Round the rugged rock the ragged rascal ran Peter Piper picked a...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED THANK YOU IN ADVANCE...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT BY THE USER AS SPECIFIED IN THEH FIRST PART OF THE QUESTION? Ask the user to enter a number and display the number, followed by ***, followed by the number squared, followed by &&&, and followed by the number cubed. Allow the user to enter as many numbers as he/she wishes. So, use a reasonable sentinel value to end the loop (for example, -999)....
Display the shortest words in a sentence in JAVA Write a method that displays all the...
Display the shortest words in a sentence in JAVA Write a method that displays all the shortest words in a given sentence. You are not allowed to use array In the main method, ask the user to enter a sentence and call the method above to display all the shortest words in a given sentence. You must design the algorithms for both the programmer-defined method and the main method.
MUST BE DONE IN C (NOT C++) In this task, using a function, we will add...
MUST BE DONE IN C (NOT C++) In this task, using a function, we will add a range of values of an array. The range will be determined by the user. For example, if I have the following array … 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells me to add from the 3rd element to the 6th element, my program would add the values 8.9, 4.6, 7.8 and 995.1. To do so, please follow...
In C language Write a program that includes a function search() that finds the index of...
In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...
Define a Python function which finds the all of the odd numbers and the product of...
Define a Python function which finds the all of the odd numbers and the product of the odd numbers. (Use for loop and if conditions for this problem. Do not use existing codes. Use your own codes).(You need to use append function to obtain a list of odd numbers)
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT