我编了一个输出字符中每个单词的长度和最长单词的长度,可一直运行不成功,求高手指点,字符是由一点结束的,谢谢!
#include<stdio.h>#include<stdlib.h>
int max(int x, int y)
{int z;
z=(x>y)?x:y;
return z;
}
main()
{
int sta,k,j;
sta=0;
k=0;
j=0;
char ch;
scanf("%c",&ch);
while (ch!='.') do
switch(sta)
{
case0: if(ch=' ')
sta=2;
else
{
sta=1;
k=k+1;
}
case1: if(ch=' ')
{
sta=2;
printf("%d",k);
j=int max(k,j);
k=0;
}
else
{
sta=1;
k=k+1;
}
case2: if(ch!=' ')
{
sta=1;
k=k+1;
}
scanf("%c",&ch);
}
printf("最长单词长度=%d",j);
system("PAUSE");
}