统计单词

 题目截图:

 

 

思路:

   依次输入单词,若单词末位为 '.' 则表示为结尾,减去末位后输出单词长度,输入停止;否则,输出单词长度,继续输入。

 

代码如下:

 1 /*
 2     统计单词 
 3 */
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <math.h>
 8 #include <stdlib.h>
 9 #include <time.h>
10 #include <stdbool.h>
11 
12 int main() {
13     char str[100];
14     while(scanf("%s", str) != EOF) {    // 输入单词 
15         int len = strlen(str);            // 单词长度 
16         if(str[len-1] == '.') {            // 若单词末位为'.',则为结尾 
17             printf("%d", len-1);        // 去掉 '.' 
18             break;
19         }
20         printf("%d ", len);                // 输出每个单词字符个数 
21     } 
22 
23     return 0;
24 }

 

posted @ 2018-02-05 15:57  Just_for_Myself  阅读(260)  评论(0编辑  收藏  举报