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 00:40
If you arrive at the same time as another user straight across from you yield if a. they flash your headlights at you b. you can’t see their turn signals c. you’re going street and they’re running d. you’re turning they’re going straight plz
Answers: 1
question
Computers and Technology, 22.06.2019 10:00
Which is a false statement considering copyright law? a. when people upload something to the internet they automatically receive a copyright for the work b. the work does not have to contain a copyright notice to be considered having a copyright c. copyright is legal term describing rights given to the creators for literary and artistic works d. personal pictures are always covered by copyrights
Answers: 1
question
Computers and Technology, 22.06.2019 15:30
When creating a budget, log fixed expenses before income. after income. after savings. at the top.
Answers: 1
question
Computers and Technology, 24.06.2019 11:00
Which of the statements below describe the guidelines for the use of text in presentation programs? a. do not use numbered lists. b. fonts should be appropriate for your audience. c. limit the number of fonts you use to three or four. d. only use bulleted lists for sales promotions. e. select font sizes that are appropriate for your delivery method. f. use font colors that work well with your background. select all that apply
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
World Languages, 08.12.2021 17:30
question
Physics, 08.12.2021 17:30
Questions on the website: 13722367