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...
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)....
● 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...
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...
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.
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all...
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all letters occurring in the parameter text. For example: if __name__ == '__main__': d = letter_frequencies('hello, world') print(d) # show the contents of this dictionary will produce the following output: {'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.1, 'e': 0.1, 'f': 0.0, 'g': 0.0, 'h': 0.1, 'i': 0.0, 'j': 0.0, 'k': 0.0, 'l': 0.3, 'm': 0.0, 'n': 0.0, 'o': 0.2, 'p': 0.0, 'q': 0.0,'r': 0.1,...
1. For all of the following words, if you move the first letter to the end...
1. For all of the following words, if you move the first letter to the end of the word and then spell the word backwards, you get the original word: banana dresser grammar potato revive uneven assess Write a program that gets a word from the keyboard and determines whether it has this property. treat uppercase and lowercase letters alike. That means that poTato also has this property. 2. Write a program that generates a random number (between 1 and...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a one-dimensional array. A sample run is below. The elements of the array are: 0 8 -4 6 7 The sum of the array is: 17 Press any key to continue . . .
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT