Написать на с++. Использовать функции. Ввести текст, состоящий из слов, разделенных пробелом. В конце текста – точка. Напечатать все слова, являющиеся палиндромами.
bool isPalindrome(std::string word) { int left = 0; int right = word.length() - 1;
while (left < right) { if (word[left] != word[right]) { return false; } left++; right--; } return true;
}
void printPalindromes(std::string text) { std::vector words; std::string word = "";
for (int i = 0; i < text.length(); i++) { if (text[i] == ' ' || text[i] == '.') { if (!word.empty()) { words.push_back(word); word = ""; } } else { word += text[i]; } } for (std::string word : words) { if (isPalindrome(word)) { std::cout << word << " "; } }
}
int main() { std::string text; std::cout << "Enter text with words separated by space and ending with a dot: "; std::getline(std::cin, text);
bool isPalindrome(std::string word) {
while (left < right) {int left = 0;
int right = word.length() - 1;
if (word[left] != word[right]) {
return false;
}
left++;
right--;
}
return true;
}
void printPalindromes(std::string text) {
for (int i = 0; i < text.length(); i++) {std::vector words;
std::string word = "";
if (text[i] == ' ' || text[i] == '.') {
if (!word.empty()) {
words.push_back(word);
word = "";
}
} else {
word += text[i];
}
}
for (std::string word : words) {
if (isPalindrome(word)) {
std::cout << word << " ";
}
}
}
int main() {
printPalindromes(text);std::string text;
std::cout << "Enter text with words separated by space and ending with a dot: ";
std::getline(std::cin, text);
return 0;
}