最长单词

code

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=200000;
char s[N];
int main(){
	int l,m;
	char *t,*w;
	if(NULL!=fgets(s,N,stdin)){
		l=strlen(s);
		if('\n'==s[l-1]){//func fgets() will take '\n' as space to a word
			s[l-1]='\0';
		}
		m=0;
		t=strtok(s," ");//slice sentence with space
		while(t){
			l=strlen(t);
			if(l>m){//if have longer word
				m=l;//updata
				w=t;//point to the adress of longer word
			}
			t=strtok(NULL," ");//get next substring
		}
		puts(w);
	}
	
	return 0;
}

ref

Q

最长单词
     
资源限制
时间限制:1.0s   内存限制:512.0MB
  编写一个函数,输入一行字符,将此字符串中最长的单词输出。
  输入仅一行,多个单词,每个单词间用一个空格隔开。单词仅由小写字母组成。所有单词的长度和不超过100000。如有多个最长单词,输出最先出现的。
样例输入
I am a student
样例输出
student
posted @ 2022-02-21 08:53  ethon-wang  阅读(44)  评论(0编辑  收藏  举报