C 语言实例 - 字符串中各种字符计算

C 语言教程 · 2019-02-21 09:42:10

计算字符串中的元音、辅音、数字、空白符。

实例

#include <stdio.h> int main() { char line[150]; int i, vowels, consonants, digits, spaces; vowels = consonants = digits = spaces = 0; printf("输入一个字符串: "); scanf("%[^\n]", line); for(i=0; line[i]!='\0'; ++i) { if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U') { ++vowels; } else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z')) { ++consonants; } else if(line[i]>='0' && line[i]<='9') { ++digits; } else if (line[i]==' ') { ++spaces; } } printf("元音: %d",vowels); printf("\n辅音: %d",consonants); printf("\n数字: %d",digits); printf("\n空白符: %d", spaces); return 0; }

输出结果为:

输入一个字符串: codercto123 erkcomsww dfcodercto
元音: 8
辅音: 15
数字: 3
空白符: 2

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

查看所有标签

How to Design Programs, 2nd Edition

How to Design Programs, 2nd Edition

Matthias Felleisen、Robert Bruce Findler、Matthew Flatt、Shriram Krishnamurthi / MIT Press / 2018-5-4 / USD 57.00

A completely revised edition, offering new design recipes for interactive programs and support for images as plain values, testing, event-driven programming, and even distributed programming. This ......一起来看看 《How to Design Programs, 2nd Edition》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

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

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试