C++找出其中的大写字母、小写字母、空格、数字以及其他字符各有多少
任务描述
输入一行文字,找出其中的大写字母、小写字母、空格、数字以及其他字符各有多少(要求用指针或引用方法处理)。
测试输入:
abcJK 116_
预期输出:
upper case:2
lower case:3
space:1
digit:3
other:1
测试输入:
QWERzxcv@#$7 8*(zz
预期输出:
upper case:4
lower case:6
space:1
digit:2
other:5
源代码:
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str;
int lowersum=0,uppersum=0,space=0,digitsum=0,othersum=0;
int i;
getline(cin,str);
for(i=0;i<str.length();i++){
if(str[i]>='a'&&str[i]<='z')lowersum++;
else if(str[i]>='A'&&str[i]<='Z')uppersum++;
else if(str[i]==' ')space++;
else if(str[i]>='0'&&str[i]<='9')digitsum++;
else othersum++;
}
cout<<"upper case:"<<uppersum<<endl;
cout<<"lower case:"<<lowersum<<endl;
cout<<"space:"<<space<<endl;
cout<<"digit:"<<digitsum<<endl;
cout<<"other:"<<othersum<<endl;
return 0;
}
原文链接: https://www.cnblogs.com/lightice/p/12691758.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/341761
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!