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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 11:00, jaygamer37
What is the name of the sound effect that danny hears
Answers: 1
image
Computers and Technology, 23.06.2019 15:00, Siris420
Plz ( which is an example of a good url?
Answers: 1
image
Computers and Technology, 23.06.2019 21:20, nicki76
In microsoft word, when you highlight existing text you want to replace, you're in              a.  advanced mode.    b.  automatic mode.    c.  basic mode.    d.  typeover mode
Answers: 1
image
Computers and Technology, 24.06.2019 20:30, jalenevoyles
Which key should you press to leave the cell as it originally was? a. delete b. cancel c. backspace d. enter
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 in other subjects: