Write a c++program that reads in some text with a character at the start of a word or line to end input The program must build up a hidden message from the first and last words of each sentence Input
#include
using namespace std;
int main() {
string input;
vector
// Read in input and split into sentences
cout << "Enter text with * at start of word/line to end input: " << endl;
while (getline(cin, input)) {
if (input[0] == '*') {
break;
}
sentences.push_back(input);
}
// Extract first and last words from each sentence
for (string sentence : sentences) {
int start = 0;
int end = sentence.find(" ");
string first_word = sentence.substr(start, end - start);
start = sentence.find_last_of(" ") + 1;
end = sentence.find_first_of(".!?") + 1;
string last_word = sentence.substr(start, end - start);
words.push_back(first_word);
words.push_back(last_word);
}
// Build up hidden message
string message = "";
for (string word : words) {
if (word != "") {
if (message != "") {
message += " ";
}
message += word;
}
}
// Output hidden message
cout << "Hidden message: " << message << endl;
return 0;
原文地址: https://www.cveoy.top/t/topic/hw5L 著作权归作者所有。请勿转载和采集!