C/C++_統計字母個數

參考:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NUM 26 /* 一共有26個字母大或小 */

int main()
{
	int i = 1;
	int a[NUM] = { 0 };
	int A[NUM] = { 0 };

	char str[] = "ddfdfdfadfasdg";

	for (i = 0; i < strlen(str); i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
		{
			A[str[i] - 'A']++;
		}
		else if (str[i] > 'a' && str[i] <= 'z')
		{
			a[str[i] - 'a']++;
		}
	}

	for (i = 0; i < 26; i++)
	{
		printf("%c=%d\n", 'a' + i, a[i]);
	}


	for (i = 0; i < 26; i++)
	{
		printf("%c=%d\n", 'A' + i, A[i]);
	}

	system("pause");
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章