Приверно так (чистой воды C++), на 100% не тестил - работа, знаете-ли.
А, да еще, знаки препинания не учитывались!!! Но думаю это добавить будет не сложно и без меня, если надо.
Код:
#include <iostream>
#include <set>
#include <string>
typedef std::set<std::string> SList;
//Функция ввода предложения
void readSentence(SList &strlist)
{
using namespace std;
strlist.clear();
cin.clear();
char buffer[1024];
cin.getline(buffer,1023);
char *si=buffer;
char *start=si;
//Split string
for (;*si!=0;si++)
{
if (' '==*si)
{
*si=0;
string str=start;
if (!str.empty())
strlist.insert(str);
start=si+1;
}
}
if (*start!=0) // Insert last word
strlist.insert(string(start));
}
//основная программа
int main(int argc, char* argv[])
{
using namespace std;
SList _list[2];
for (int i=0;i<2;i++)
{// Воодим предложения
cout << endl << "enter " << i << "sentence" << endl;
readSentence(_list[i]);
}
if (_list[0].empty() || _list[1].empty())
{ //если хотя бы одно предложение пустое.
cout << endl << "No words found!";
}
else
{ // ищем
SList::const_iterator iter=_list[0].begin();
SList::const_iterator enditer=_list[0].end();
size_t maxlen=0;
string needWord;
for (;iter!=enditer;++iter) // просматриваем все слова одного из предложений
{
if (_list[1].find(*iter)!=_list[1].end()) //сово есть во втором
{
if (maxlen<(*iter).size()) //смотрим, длинее ли оно ранее найденного
{
maxlen=(*iter).size();
needWord=*iter;
}
}
}
if (needWord.empty()) //выводим резульата поиска.
cout << endl << "No word found!";
else
cout << endl << "Found word: '" << needWord.c_str() << "'" << endl;
}
return 0;
}