本题目要求编写程序,输入一行字符,统计每个单词的长度。所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。
输入格式:
输入给出一行字符。
输出格式:
在一行中输出每个单词的长度。每个数字后有一个空格。
输入样例:
How are you?
结尾无空行
输出样例:
3 3 4
结尾无空行
#include<>
#include<>
int main(){
char ch;
int state=0,len=0;
while(1){
ch=getchar();
if(ch!=' '&&state==0&&ch!='\n'){
len++;
state=1;
}
else if(ch!=' '&&state==1&&ch!='\n'){
len++;
}
else if(ch==' '&&state==1&&ch!='\n'){
printf("%d ",len);
state=0;
len=0;
}
else if(ch=='\n'&&state==1){
printf("%d ",len);
break;
}
else if(ch=='\n'&&state==0){
break;
}
}
}