使用 C 语言查找字符串中的字母、数字和特殊字符的数量
以下是我们实现字母、数字和特殊字符查找的逻辑 -
for(number=0;string[number]!='\0';number++) {// for loop until endof string
if(string[number]>='a'&&string[number]<='z'||string[number]>='A'&&string[number]<='Z') //checking alphabets in string{
alphabets=alphabets+1; //counting alphabets
//alphabets++;
}
else if(string[number]>='0'&&string[number]<='9'){ //checking numbers in string
digits=digits+1; //counting numbers
//digits++;
} else {
special=special+1; //counting special characters
//special++;
}
}以下程序用于识别字符串中字母、数字和特殊字符的总数 -
示例
#include<stdio.h>
#include<ctype.h>
void main(){
//Declaring integer for number determination, string//
int number;
char string[50];
int alphabets=0;
int digits=0;
int special=0;
//Reading User I/p//
printf("Enter the string :");
gets(string);
for(number=0;string[number]!='\0';number++){
if(string[number]>='a'&&string[number]<='z'||string[number]>='A'&&string[number]<='Z'){
alphabets=alphabets+1;
//alphabets++;
}
else if(string[number]>='0'&&string[number]<='9'){
digits=digits+1;
//digits++;
}
else{
special=special+1;
//special++;
}
}
//Printing number of alphabets, number of digits, number of special characters//
printf("The number of alphabets in the string is : %d
",alphabets);
printf("The number of digits in the string is : %d
",digits);
printf("The number of special characters in the string is : %d
",special);
}输出
Enter the string :The number of alphabets in the string is : 0 The number of digits in the string is : 0 The number of special characters in the string is : 1
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP