统计字符串中的字符

#include<iostream>
#include<cstdlib>
#include<cstring>
#define maxn 1000
using namespace std;
void stat(char[]);
char str[maxn];
int main()
{
	gets_s(str);
	stat(str);
	system("pause");
	return 0;
}
void stat(char s[])
{
	int num = 0, letter = 0, space = 0,others=0;
	for (int i = 0; s[i] !='\0'; i++)
	{
		if (s[i] >= '0'&&s[i] <= '9') num++;
		else if ((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z')) letter++;
		else if (s[i] == ' ') space++;
		else others++;
	}
	cout << "num:\t" << num << endl;
	cout << "letter:\t" << letter << endl;
	cout << "space:\t" << space << endl;
	cout << "others:\t" << others << endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_43411988/article/details/84306542