192k views
2 votes
write a program that finds word differences between two sentences. the input begins with the first sentence and the following input line is the second sentence. assume that the two sentences have the same number of words. the program displays word pairs that differ between the two sentences. one pair is displayed per line. ex: if the input is: smaller cars get better gas mileage tiny cars get great fuel economy then the output is: smaller tiny better great gas fuel mileage economy

User Teqnology
by
8.1k points

1 Answer

1 vote

Answer:

#include <iostream>

#include <string>

using namespace std;

int main() {

string sentence1, sentence2, word1, word2;

cout << "Enter the first sentence: ";

getline(cin, sentence1);

cout << "Enter the second sentence: ";

getline(cin, sentence2);

stringstream s1(sentence1), s2(sentence2);

while (s1 >> word1 && s2 >> word2) {

if (word1 != word2) {

cout << word1 << " " << word2 << endl;

}

}

return 0;

}

Step-by-step explanation:

two strings sentence1 and sentence2 are read from the user. The stringstream class is used to extract words from the sentences, one word at a time, and store them in the word1 and word2 variables. The >> operator is used to extract words from the stringstreams, and the while loop continues until either stringstream is empty. If the words word1 and word2 are not equal, they are displayed as a word pair on a separate line.

User Nathan Gouy
by
7.9k points