题解 | 字母统计

字母统计

https://www.nowcoder.com/practice/de7bf0945c1c4bd1aa9d49573b831f3c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <cstring>
#include<vector>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
//输入一行字符串,计算其中A-Z大写字母出现的次数

//案例可能有多组,每个案例输入为一行字符串。

//对每个案例按A-Z的顺序输出其中大写字母出现的次数。
int main()
{
	char str[100];
	while (scanf("%s", str) != EOF)
	{
		map<char, int> myMap =
		{
			{'A',0},{'B',0},{'C',0},{'D',0},{'E',0},
			{'F',0},{'G',0},{'H',0},{'I',0},{'J',0},
			{'K',0},{'L',0},{'M',0},{'N',0},{'O',0},
			{'P',0},{'Q',0},{'R',0},{'S',0},{'T',0},
			{'U',0},{'V',0},{'W',0},{'X',0},{'Y',0},
			{'Z',0}
		};
		
		for (int i = 0; i < strlen(str); i++)
		{
			if (str[i] >= 'A' && str[i] <= 'Z')
			{
				myMap[str[i]]++;
			}
		}
		map<char, int>::iterator it;
		for (it = myMap.begin(); it != myMap.end(); it++)
		{
			printf("%c:%d\n", it->first, it->second);
		}

	}
	return 0;
}

全部评论

相关推荐

葬爱~冷少:我当时都是上午刷力扣,下午背八股,有活给我先别急,没活就干自己的事情
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务