In: Computer Science
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)
#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; }