subject

5.29 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 wordList, string wordToFind).

This is what i have.
#include

#include

using namespace std;

int FindWordInWordList(vector wordList, string wordToFind);

int main()

{

int a, b, i ;

vector word, replace;

string str ;

cin >> a ;

for(i = 0; i < a; i++)

{

cin >> str;

word. push_back(str);

cin >> str ;

replace. push_back(str) ;

}

cin >> b;

for(i = 0; i < b; i++)

{

cin >> str ;

int pos = FindWordInWordList(word, str) ;

if(pos == -1)

cout << str ;

else

cout << replace[pos] ;

if(i != b-1)

cout << " " ;

}

cout << endl ;

return 0;

}

int FindWordInWordList(vector wordList, string wordToFind)

{

for(int i = 0; i < wordList. size(); i++)

{

if(wordList[i] == wordToFind)

return i;

}

return -1;

}

the only problem i am having is putting a space at the end of the sentence.

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 18:30
Which of these options are the correct sequence of actions for content to be copied and pasted? select content, click the copy button, click the paste button, and move the insertion point to where the content needs to be inserted. click the copy button, select the content, move the insertion point to where the content needs to be inserted, and click the paste button. select the content, click the copy button, move the insertion point to where the content needs to be inserted, and click the paste button. select the content, move the insertion point to where the content needs to be inserted, click the copy button, and click the paste button.
Answers: 3
question
Computers and Technology, 22.06.2019 20:00
Awide variety of “ apps “ are available to customize devices. which category of app does the word processing software fall into?
Answers: 2
question
Computers and Technology, 22.06.2019 20:30
In this lab, you complete a prewritten c program that calculates an employee’s productivity bonus and prints the employee’s name and bonus. bonuses are calculated based on an employee’s productivity score as shown below. a productivity score is calculated by first dividing an employee’s transactions dollar value by the number of transactions and then dividing the result by the number of shifts worked.
Answers: 3
question
Computers and Technology, 23.06.2019 15:10
What role did women fill during world war ii?
Answers: 1
You know the right answer?
5.29 LAB: Replacement words
Write a program that replaces words in a sentence. The input begin...
Questions
question
Chemistry, 03.11.2020 01:00
question
Biology, 03.11.2020 01:00
question
Mathematics, 03.11.2020 01:00
question
Business, 03.11.2020 01:00
question
Mathematics, 03.11.2020 01:00
question
Mathematics, 03.11.2020 01:00
question
Mathematics, 03.11.2020 01:00
Questions on the website: 13722361