C 练习实例17

C 语言教程 · 2019-02-21 16:12:10

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

程序分析:利用while语句,条件为输入的字符不为'\n'。

实例

// Created by www.codercto.com on 15/11/9. // Copyright © 2015年 码农教程. All rights reserved. // #include<stdio.h> int main() { char c; int letters=0,spaces=0,digits=0,others=0; printf("请输入一些字母:\n"); while((c=getchar())!='\n') { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) letters++; else if(c>='0'&&c<='9') digits++; else if(c==' ') spaces++; else others++; } printf("字母=%d,数字=%d,空格=%d,其他=%d\n",letters,digits,spaces,others); return 0; }

以上实例输出结果为:

请输入一些字母:
www.codercto.com 123
字母=12,数字=3,空格=1,其他=2

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

Numerical Recipes 3rd Edition

Numerical Recipes 3rd Edition

William H. Press、Saul A. Teukolsky、William T. Vetterling、Brian P. Flannery / Cambridge University Press / 2007-9-6 / GBP 64.99

Do you want easy access to the latest methods in scientific computing? This greatly expanded third edition of Numerical Recipes has it, with wider coverage than ever before, many new, expanded and upd......一起来看看 《Numerical Recipes 3rd Edition》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具