Question

In: Computer Science

c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input...

c++

9.39 LAB: Replacement words

Write a program that replaces words in a sentence. The input begins with an integer indicating the number of word replacement pairs (original and replacement) that follow. The next line of input begins with an integer indicating the number of words in the sentence that follows. Any word on the original list is replaced.

Ex: If the input is:

3   automobile car   manufacturer maker   children kids
15 The automobile manufacturer recommends car seats for children if the automobile doesn't already have one.

then the output is:

The car maker recommends car seats for kids if the car doesn't already have one. 

You can assume the original words are unique. For coding simplicity, follow each output word by a space, even the last one.

Hint: For words to replace, use two vectors: One for the original words, and the other for the replacements.

Your program must define and call the following function that returns index of word's first occurrence in wordList. If not found, then the function returns -1.
int FindWordInWordList(vector<string> wordList, string wordToFind)

Solutions

Expert Solution

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int FindWordInWordList(vector<string> wordList, string wordToFind);

int main() {
    vector<string> wordList, modified;
    int numReplacements, numOriginal;
    vector<string> original;
    string data;

    cin >> numReplacements;
    for (int i = 0; i < numReplacements; i++) {
        cin >> data;
        wordList.push_back(data);
        cin >> data;
        modified.push_back(data);
    }

    cin >> numOriginal;
    for (int i = 0; i < numOriginal; i++) {
        cin >> data;
        original.push_back(data);
    }

    for (int i = 0; i < numOriginal; i++) {
        int index = FindWordInWordList(wordList, original[i]);
        if (index == -1)
            cout << original[i] << " ";
        else
            cout << modified[index] << " ";
    }
    cout << endl;

    return 0;
}

int FindWordInWordList(vector<string> wordList, string wordToFind) {
    for (int i = 0; i < wordList.size(); i++) {
        if (wordList[i] == wordToFind)
            return i;
    }
    return -1;
}


Related Solutions

how to write a c++ program that replaces certain words with two "**" input: I went...
how to write a c++ program that replaces certain words with two "**" input: I went to the store To buy some fruit Apples are red I like oranges better I saw grapes are green apples are red, oranges, grapes are green (sub strings that need to be replaced) iterate through the string input and get an output that replaces the sub strings with ** If the sub strings are not in the input just return the string as it...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT