很简单,读入整个字符串,然后逐个扫描单词,碰到更长的就记录下来,最后输出这个最长的就行。刚开始学c++的同学可能看不懂strcpy(c,&s[start]),这是一个指针的引用。
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s[600] = {};
cin.getline(s,600);
int l = strlen(s);
int start = 0 ,end;
int max = 0;
char c[100] = {};
for (int i = 0 ; i < l ; i ++)
{
if (s[i] == ' ' || s[i] == '.')
{
end = i;
if (max < end - start)
{
max = end - start;
s[end] = 0;
strcpy(c,&s[start]);
}
start = ++i;
}
}
cout<<c<<endl;
return 0;
}