```
/输入一段文字
输入一段字符,检测出最长的单词,所处最长单词的字符串字符数以及该单词,以stop作为最后输入的字符串。
测试说明
样例输入:
My name is Amy
My name is Jane
stop
样例输出:
11 name
12 name
提示:
字符串长度不超过100。/
#include<stdio.h>
#include<string.h>
int main( )
{
while(1)
{
int max=0,i;
char a[100]={0},s[100]={0};
scanf("%s",a);
if(getchar()=='\n'&&strcmp(a,"stop")!=0) {printf("%d %s\n",max,a);}
else if(strcmp(a,"stop")==0) {break;}
else
{
if(max<strlen(a))
{
max=strlen(a);
for(i=0;i<strlen(a);i++) {s[i]=a[i];}
}
}
}
return 0;
} ```
这题这样写,供参考:
#include <stdio.h>
#include <string.h>
int main()
{
int lenw, lens, maxl;
char str[100], word[100], * pword;
while (1){
lenw = lens = maxl = 0;
gets(str);
if (!strcmp(str, "stop")) break;
pword = strtok(str, " ");
while (pword) {
lenw = strlen(pword);
lens += lenw;
if (lenw > maxl) {
maxl = lenw;
strcpy(word, pword);
}
pword = strtok(NULL, " ");
}
printf("%d %s\n", lens, word);
}
return 0;
}
你的题目是什么,是输出这句话中最长的单词还是字典序最大的单词?
不知道你这个问题是否已经解决, 如果还没有解决的话:根据参考资料,可以推测出可能出现问题的地方是输入过程中缓冲区存在其他字符。解决方法可以有如下几种:
while(getchar() != '\n');
fflush(stdin); // windows平台下采用
__fpurge(stdin); // linux平台下采用
需要具体问题具体分析,也可以结合具体代码来进行调试和寻找问题出现的原因。