Другие языки программирования и технологии

Помогите написать задачу по C++ Дана строка. найти в ней самое длинное слово-палиндром.

#include <iostream>
#include <cctype>
using namespace std;

bool is_poly(const char* f, const char* l){
if(f < l){
--l;
}
while((f < l) && (*f == *l)){
++f;
--l;
}
return (*f == *l);
}

const char* max_polyw(const char* str, const char*& end){
const char* off, *ptr = NULL;
int cur, len = 0;

end = NULL;
while(*str){
while(*str && !isalpha(*str)){
++str;
}
for(off = str; isalpha(*off); ++off);

if(is_poly(str, off)) {
cur = (int)(ptrdiff_t)(off - str);
if(cur > len){
len = cur;
ptr = str;
end = off;
}
}
str = off;
}
return ptr;
}

int main(void){
char str[] = "apl, bla-bal ada, (AKQKA) bob, awa,.";

const char* last;
const char* first = max_polyw(str, last);

if(first < last){
cout.write(first, (int)(last - first));
}
cin.get();
return 0;
}
Лютик *****
Лютик *****
11 372
Лучший ответ
Вот пример кода в C++:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main(void) {
string yourString, word;
vector <string> palindromes;
unsigned int maxPalindromeSize = 0;
cout << "Введите строку текста: " << endl;
getline(cin, yourString);
stringstream streamForString(yourString);
while(streamForString >> word) {
string invertWord(word);
reverse(invertWord.begin(), invertWord.end());
if ((!word.compare(invertWord)) && (word.size() > 1)) {
palindromes.push_back(word);
if (word.size() > maxPalindromeSize)
maxPalindromeSize = word.size();
}
}
if (palindromes.empty())
cout << "В строке нет палиндромов. ";
else {
cout << "Самые длинные палиндромы из " << maxPalindromeSize << " символов в строке: " << endl;
for (unsigned int i = 0; i < palindromes.size(); i++)
if (palindromes[i].size() == maxPalindromeSize)
cout << " - " << palindromes[i] << endl;
}
return cin.get();
}
Михаил Слепов
Михаил Слепов
2 069