使用字符串概念统计元音、数字、空格和辅音的C程序
字符数组(或)字符集合称为字符串。
声明
参考以下声明:
char stringname [size];
例如:char a[50]; 长度为50个字符的字符串。
初始化
初始化如下:
- 使用**单个**字符常量:
char a[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}- 使用**字符串**常量:
char a[10] = "Hello":;
访问
使用控制字符串“%s”访问字符串,直到遇到‘\0’。
统计**元音数量**的逻辑如下:
if(string[i]=='a'||string[i]=='e'||string[i]=='i'|| string[i]=='o'||string[i]=='u')//checking the char is vowel vowel=vowel+1;
统计**数字数量**的逻辑如下:
if(string[i]=='0'||string[i]=='1'||string[i]=='2'|| string[i]=='3'||string[i]=='4'||string[i]=='5'|| string[i]=='6'||string[i]=='7'||string[i]=='8'||string[i]=='9') digit=digit+1;
统计**空格数量**的逻辑如下:
if(string[i]==' ') space=space+1;
其余均为辅音。
程序
以下是使用字符串概念**统计元音、数字、空格和辅音的C程序**:
#include<stdio.h>
int main(){
char string[50];
int i,vowel=0,digit=0,space=0,consonant=0;
printf("enter any string includes all types of characters:
");
gets(string);
for(i=0;string[i]!='\0';i++){
if(string[i]=='a'||string[i]=='e'||string[i]=='i'||
string[i]=='o'||string[i]=='u')//checking the char is vowel vowel=vowel+1;
else if(string[i]=='0'||string[i]=='1'||string[i]=='2'||
string[i]=='3'||string[i]=='4'||string[i]=='5'||
string[i]=='6'||string[i]=='7'||string[i]=='8'||string[i]=='9')
digit=digit+1;
else if(string[i]==' ')
space=space+1;
else
consonant=consonant+1;
}
printf("vowel=%d
digit=%d
space=%d
consonant=%d
",vowel,digit,space,consonant);
return 0;
}输出
输出如下:
enter any string includes all types of characters: Tutorials Point 1234 C programming 2020 vowel=9 digit=8 space=5 consonant=17
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP