C/C++

Как найти самое длинное слово в строке C++ ?

Написать программу на С++ для определения самого длинного слова в строке.
// Самых длинных слов в строке может быть несколько!

#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main() {
system("chcp 1251 > nul");
cout << "Строка: ";
string line;
getline(cin, line);
istringstream iss(line);
string word;
vector<string> box;
while (iss >> word) box.push_back(word);
auto max_length = [](string& a, string& b) {
return a.length() > b.length();
};
sort(box.begin(), box.end(), max_length);
cout << "Самые длинные слова:";
int n = 0;
for (const auto& x : box) {
if (x.length() == box.front().length()) {
if (n++) cout.put(';');
cout << ' ' << x;
} else {
cout.put('.');
break;
}
}
system("pause > nul");
}
Александр Кочерыгин
Александр Кочерыгин
82 128
Лучший ответ
#include "iostream"
#include "sstream"
#include "string"
using namespace std;
int main(){
string s,t,r=""; getline(cin,s); istringstream iss(s);
for(; iss>>t; t.length()>r.length()?r=t:r); cout<<r<< endl;}
Андрей Арбузов
Андрей Арбузов
84 012
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
string str;
getline(cin, str);
int i_max = 0;
int len = 0;
int max_len = 0;
for(int x = 0; x < str.length(); x++)
{
if( isalpha(str[x]) )
{
len++;
if(!isalpha(str[x+1]))
{
if(len > max_len)
{
max_len = len;
i_max = x + 1 - len;
}
}
}
else
{
len = 0;
}
}

cout << "Самое длинное слово -- ";
for(int x = i_max; x < (i_max + max_len); x++)
{
cout << str[x];
}
cout << '.' << endl;

return 0;
}

// Программа работает только с английскими словами.
Самат Караулов
Самат Караулов
54 865
Вячеслав Путылин Спасибо за ответ. Эту задачу я уже решил другим способом, нашел в интернете. Не могли бы вы пожалуйста посмотреть еще мой предыдущий вопрос? Там тоже работа со строками
#include<stdio.h>
#include<ctype.h>
#include<unistd.h>
int main()
{
char string[1024];
int count=0, len=0,maxlen=0;
char *ps=string;
char *plonger;
printf("Enter your string:\n");
fgets(string,1024,stdin);

while(*ps)
{
if(isspace(*ps)&&(*ps)||(*ps==',')
||(*ps=='.')||(*ps==';')||(*ps==':'))
{
while(isspace(*ps)&&(*ps)||(*ps==',')
||(*ps=='.')||(*ps==';')||(*ps==':'))
ps++;
}

if(!isspace(*ps)&&(*ps)&&(*ps!=',')
&&(*ps!='.')&&(*ps!=';')&&(*ps!=':'))
{ //if
len=0;
while(!isspace(*ps)&&(*ps)&&(*ps!=',')
&&(*ps!='.')&&(*ps!=';')&&(*ps!=':'))
{ //while
ps++;
len++;
} //while
count++;
if(len>maxlen)
{ maxlen=len; plonger=ps-len;}
}//if

}

printf("\nLonger word of %d symbols",maxlen);
printf("\nthis is word: ");
for(int i = 0; i < maxlen; i++)
printf("%c",*plonger++);
printf("\n\n");
fflush(stdout);
sleep(10);
return 0;
}
Вячеслав Путылин Спасибо за ответ. Эту задачу я уже решил другим способом, нашел в интернете. Не могли бы вы пожалуйста посмотреть еще мой предыдущий вопрос? Там тоже работа со строками
Вопрос в чём?
Вячеслав Путылин Как найти самое длинное слово в строке C++ ?
Посчитать кол-во char
Вячеслав Путылин Спасибо за ответ. Эту задачу я уже решил, нашел в интернете. Не могли бы вы пожалуйста посмотреть еще мой предыдущий вопрос? Там тоже работа со строками