匿名用户
1级
2013-05-06 回答
这一行写错了,p1前要加个*号:
printf("%c",p1++);
要改成
printf("%c", *p1++);
看别人写的程序很难懂,不如自己重写一下,已经测试通过:
#include
#include
int main()
{
char s[128];
char *p1, *p2;
int max=0, len=0;
printf("Input a string: ");
gets(s);
p1=s;
for (int i=0; i
{
if (s[i]==' ') // 如果当前字符为空格,则比较当前单词长度是否大于最大值,再将长度复位。
{
if (len>max)
{
max=len;
p2=p1;
}
len=0;
} else // 如果当前字符非空,如果当前长度为0,则表示新单词。
{
if (len==0)
p1=&s[i];
++len;
}
}
while (*p2 && *p2!=' ')
printf("%c", *p2++);
}
追问:
额,你的程序我也觉得没问题,但运行的结果有错误,比如我输入the people,结果输出是the,不符合要求啊
追答:
抱歉,是没把字符串结束符考虑进去,稍微改一下,请测试:
#include
#include
int main()
{
char s[128];
char *p1, *p2;
int max=0, len=0;
printf("Input a string: ");
gets(s);
p1=s;
for (int i=0; imax)
{
max=len;
p2=p1;
}
len=0;
} else // 如果当前字符非空,如果当前长度为0,则表示新单词。
{
if (len==0)
p1=&s[i];
++len;
}
}
while (*p2 && *p2!=' ')
printf("%c", *p2++);
}