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://codercto.com/courses/l/17.html

查看所有标签

算法基础

算法基础

布拉萨德 / 邱仲潘 / 清华大学出版社 / 2005-7 / 49.00元

本书是关于算法导论的经典教材,书中包括大量例题解答与命题证明。本书是按照算法类型而不是按照应用类型对算法进行介绍,以其清晰的概念讲解赢得专家们的广泛赞誉。本书适用对象广泛。对于学习算法设计与分析的本科生和研究生,本书是优透选教材。对于从事算法计算研究和工程应用的科研人员和工程技术人员,本书也是一本优秀的基础性读物。一起来看看 《算法基础》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

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

正则表达式在线测试