我们给了一个包含大写字母,小写字母,特殊字符和数字值的字符串。任务是计算字符串中所有类型的字符,特殊字符和数字值的频率。
大写字母-A-Z的ASCII值介于65-90之间,其中65和90(含)在内。
小写字母− a – z的ASCII值介于97-122之间,其中97和122(包括)。
数值-0-9,ASCII值范围为48-57,其中48和57(含)。
特殊字符-!,@,#,$,%,^,&,*
输入− str = Tutori @ lPo!n&90
输出-字符串中的大写字母总数为-2
字符串中的小写字母总数为− 8
字符串中的总数为− 2
字符串中的特殊字符总数为− 3
输入-str = WELc0m $
输出-字符串中的大写字母总数为-3
字符串中的小写字母总数为− 2
字符串中的总数为− 1
字符串中的特殊字符总数为− 1
输入包含大写字母,小写字母,特殊字符和数字值的字符串。
计算字符串的长度
使用变量来存储大写字母,小写字母,特殊字符和数字的计数,并将其初始化为0。
从0开始启动FOR循环,直到字符串大小
在循环内部,检查IF str [i]> = A和str [i] <= Z,然后增加大写字母的计数。
在循环内部,检查IF str [i]> = a和str [i] <= z,然后增加小写字母的计数。
在循环内部,检查IF str [i]> = 0和str [i] <= 9,然后增加数值的计数。
否则,增加特殊字符的数量。
打印结果
#include<iostream>
using namespace std;
//计算大写,小写,特殊字符和数值
void count(string str){
int Uppercase = 0;
int Lowercase = 0;
int digit = 0;
int special_character = 0;
for (int i = 0; i < str.length(); i++){
if (str[i] >= 'A' && str[i] <= 'Z'){
Uppercase++;
}
else if(str[i] >= 'a' && str[i] <= 'z'){
Lowercase++;
}
else if(str[i]>= '0' && str[i]<= '9'){
digit++;
}
else{
special_character++;
}
}
cout<<"Total Upper case letters in a string are: "<<Uppercase<< endl;
cout<<"Total lower case letters in a string are: "<<Lowercase<< endl;
cout<<"Total number in a string are: "<<digit<< endl;
cout<<"total of special characters in a string are: "<<special_character<< endl;
}
int main(){
string str = "Tutori@lPo!n&90";
count(str);
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
Total Upper case letters in a string are: 2 Total lower case letters in a string are: 8 Total number in a string are: 2 total of special characters in a string are: 3