题目描述
编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。 (凡是以一个或多个空格隔开的部分就为一个单词)
输入描述:
输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。
输出描述:
可能有多组测试数据,对于每组数据,输出字符串中每个单词包含的字母的个数。输入
hello how are you.
输出
5 3 3 3
code:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
char s[500];
int a[100];
while(gets(s)!=NULL){
int len = strlen(s);
int j = 0;
int t = 0;
for(int i = 0;i<len;i++){
if(s[i]!=' '){
t++;
}
if(s[i]!=' '&&(s[i+1]==' '||s[i+1]=='.')){
a[j++] = t;
t = 0;
}
}
for(int i = 0;i<j;i++){
if(i == 0)
printf("%d",a[i]);
else
printf(" %d",a[i]);
}
printf("\n");
}
}