首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >C++:统计字符串内大小写字母,数字空格,逗号等

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

作者头像
川川菜鸟
发布于 2021-10-18 11:20:57
发布于 2021-10-18 11:20:57
1.5K0
举报

川川好久没发文了,最近实在太忙,要是感兴趣可以直接跟我私聊, QQ:2835809579

原题: 用赋初值的方法把字符串 “ 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中,编程统计其中的大写字母、小写字母、数字、空格、逗号的个数。

代码:

代码语言:javascript
复制
#include
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;

}

有问题,留言或者加我扣扣问我吧。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/12/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档