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
● 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,...
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...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Write a C++ function template to add two inputs and return their result. Make exceptions for...
Write a C++ function template to add two inputs and return their result. Make exceptions for Characters (such that sum of two characters is a character associated with sum of their ASCII) and String (such that sum of two strings is their concatenation)
Write a function sin_x_crossings(begin, end, skip) that uses sin math function which takes begin, end and...
Write a function sin_x_crossings(begin, end, skip) that uses sin math function which takes begin, end and skip as input parameters, and returns the x-points as a Python list that crosses the x-axis (i.e., x-value that just before it crosses). Hint: see axis-crossing lecture notes for hints. Note: you cannot use for or while loops. Note2: numpy has been imported as np For example: Test Result import math ans = sin_x_crossings(0, 4 * math.pi, 0.01) for val in ans: print(round(val, 2))...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT