0
点赞
收藏
分享

微信扫一扫

C++:统计字符串内大小写字母,数字空格,逗号等

紫荆峰 2022-06-09 阅读 64

川川好久没发文了

原题:
用赋初值的方法把字符串 “ C is a general purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.” 存放到字符数组s中,编程统计其中的大写字母、小写字母、数字、空格、逗号的个数。

代码:

#include<iostream>
using namespace std;

int main()

{
char c;
int smallletters = 0, largeletters=0,space = 0, digit = 0,douhao=0, other = 0;
cout << "请输入字符串:";
while ((c = getchar()) != '\n')
{
if (c >= 'a' && c <= 'z')
{
smallletters++;
}
else if (c >= 'A' && c <= 'Z')
{
largeletters++;
}
else if (c == ' ')
{
space++;
}
else if (c >= '0' && c <= '9')
{
digit++;
}
else if (c == ',')
{
douhao++;
}
else
{
other++;
}
}
cout <<"小写字母:"<< smallletters <<"\t"<<"大写字母:"<<largeletters<<"\t"<<"空格:"<< space <<"\t"<<"数字:"<< digit <<"\t"<<"逗号:"<<douhao<<"\t"<<"其它:"<< other <<"\t"<< endl;

return 0;

}



举报

相关推荐

0 条评论