Jimmy小站
小明也有大梦想 — 蒋明/铭6-9 编程题,统计一行字符中单词的个数
2015-08-30 / C语言基础题 / 3450 次围观 / 0 次吐槽【问题描述】
输入一行字符,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。
【输入形式】
从键盘输入一行字符。
【输出形式】
输出单词的个数。
【样例输入1】(下划线部分为键盘输入,其余部分为程序输出)
Input words: Let’s go to room 209.
【样例输出1】
count=5
【样例输入2】(下划线部分为键盘输入,其余部分为程序输出)
Input words: You and me are friend.
【样例输出2】
count=5
【样例说明】
输入提示符后要加一个空格。例如“Input integers: ”,其中“:”后要加一个且只能一个空格。
英文字母区分大小写。必须严格按样例输入输出。
#include<stdio.h> int main(void) { char ch; int word=0,sum=0; printf("Input words: "); while((ch=getchar())!='\n'){ if(ch==' ') word=0; else if(word==0){ word=1; sum=sum+1; } } printf("count=%d",sum); return 0; }
推荐您阅读更多有关于“”的文章
本月热文
Copyright © Jimmy小站 Allrights Reserved.备案号:桂ICP备 15005996
额 本文暂时没人评论 来添加一个吧
发表评论