鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: er88438

输入任意一个英文句子,输出最长单词。

[复制链接]
发表于 2023-1-10 16:49:22 | 显示全部楼层
#include
using
namespace
std;
const
int
max
=
20;
int
main()
{
unsigned
int
maxlen
=
0;
int
len
=
0;
int
i=0;
string
str[max];
cout
<<
"请输入一段字符串(ctrl+z)完成输入:\n";
getline(cin,str[i],'
');
while
(cin)
{
if
(maxlen
<
str[i].size())
{
maxlen
=
str[i].size();
len
=
i;
}
++i;
getline(cin,str[i],'
');
}
cout
<<
"最长字符串味:\n";
cout
<<
"第
"
<<
i
<<
"个,字符长度为:"
<<
maxlen
-
1
<<
"
\n字符串为:"
<<
str[len]
<<
endl;
system("pause");
}
下面是使用了vector
的方法
#include
#include
#include
using
namespace
std;
int
main()
{
vector
sv;
string
str;
int
maxlen
=
0;
int
len
=
0;
cout
<<
"请输入一段字符串(ctrl+z)完成输入:\n";
getline(cin,str,'
');
while
(cin)
{
sv.push_back(str);
if
(maxlen
<
str.size())
{
maxlen
=
str.size();
len
=
sv.size();
}
getline(cin,str,'
');
}
cout
<<
"共有"
<<
sv.size()
<<
"个词组\n"
;
cout
<<
"最长字符串味:\n";
cout
<<
"第
"
<<
len
<<
"个,字符长度为:"
<<
maxlen
<<
"
\n字符串为:"
<<
***.at(len-1)
<<
endl;
system("pause");
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 11:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表