VJ_句中最长的单词_stringstream

输入一个英文句子,长度不超过40个字符。编写程序,输出句子中最长的一个单词。

Input

长度不超过40的字符串

Output

句中最长的单词

Sample

Inputcopy Outputcopy
This is a test sentence
sentence

Hint

1.输入只有一个句子,不需使用while
2.若句尾有标点,则标点和最后一个单词可看成是一个单词,可以不用作额外判断
3.假设句中最长的单词只有一个

//
#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s,ans;
    
    while( getline( cin,s ) )
    {
        stringstream ss; ss.clear(); ss.str("");
        ss<<s;
        ans="";
        while( ss>>s )
        {
            if( ans.size() < s.size() ) ans=s;
        }
        cout<<ans<<endl;
    }
    return 0;
}

扫描二维码关注公众号,回复: 14232366 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/125130099
今日推荐