LB_8_Maks
unknown
c_cpp
3 years ago
941 B
13
Indexable
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string input;
cout << "Enter a sentence: ";
getline(cin, input);
istringstream iss(input);
string word;
int wordCount = 0;
int middleWordIndex = -1;
// Count words and find the middle word index
while (iss >> word) {
wordCount++;
}
if (wordCount > 0) {
middleWordIndex = wordCount / 2;
}
// Print the sentence with the middle word highlighted
iss.clear();
iss.seekg(0, ios::beg);
wordCount = 0;
cout << "Highlighted sentence: ";
while (iss >> word) {
if (wordCount == middleWordIndex) {
cout << "\033[1;31m" << word << "\033[0m"; // ANSI escape code for red text
} else {
cout << word;
}
cout << ' ';
wordCount++;
}
cout << endl;
return 0;
}Editor is loading...