题目
编写一个函数,输入一行字符,将此字符串中最长的单词输出。
输入仅一行,多个单词,每个单词间用一个空格隔开。单词仅由小写字母组成。所有单词的长度和不超过100000。如有多个最长单词,输出最先出现的。
输入
无
输出
无
样例输入
I am a student
样例输出
student
解题思路
不断读入单词,若新单词数目长于以往读入的最长单词,那么单词最长长度和最长单词内容都进行更新,最后输出最长单词。
代码
#include<stdio.h>
#include<string.h>
int main()
{
char temp[100001],word[100001];
int len,max = 0;
while (scanf("%s",temp)!=EOF){
len = strlen(temp);
if (len>max)
{
max = len;
strcpy(word,temp);
}
}
printf("%s",word);
return 0;
}