时间限制 : 1 秒
内存限制 : 128 MB
编写一个程序,使其能根据输入得到符合要求的输出内容。
程序须包含一个函数,该函数能求出一个字符串中最长的单词并输出。
[10.29增加] :
①提示:"若认为判题机判定结果错误请检查数组是否越界"---->即定义数组的时候定义的大一点,否则本题的检测点句子稍微长一点就可能导致数组越界,导致在自己的电脑上是正常但是OJ上是错的。
②修复了关于OJ上的空格并非正常输入的空格的问题,现在可以通过正常的空格进行检测。
输入
一行可以包含 英文大、小写字母、空格 的字符串。
输出
通过调用函数,输出此字符串中最长的单词。
样例
输入
Einstein had been a famous physicist after then but he still wore the same old overcoat
输出
physicist
//程序已AC,运用了库函数中关于字符串处理的几个函数 strlen, strncpy strtok
#include<stdio.h>
#include<string.h>
#define N 10000
char sourcestr[N];
char resultstr[N];
char *maxword(char *src){
int maxlen = 0;
int len = 0;
char s[2] = " ";//分隔符
char *token;
//获取第一个字符串
token = strtok(src, s);
//获取第一个字符串的长度,假定最长
maxlen = strlen(token);
//继续获取其它子字符串
strncpy(resultstr, token, maxlen);
while ((token =strtok(NULL, s) )!= NULL) {
len = strlen(token);
if (len > maxlen) strncpy(resultstr, token, len);
}
return resultstr;
}
int main(){
gets(sourcestr);
printf("%s",maxword(sourcestr));
return 0;
}