2.输入一行文字,找出其中的大写字母、小写字母、空格、数字、以及其他字符各有多少。
#include<iostream> //2.输入一行文字,找出其中的大写字母、小写字母、空格、数字以及其他字符各有多少
#define N 100
using namespace std;
int main()
{
void ct(char* p);
char str[N];
cout << "输入一行文字:" << endl;
gets_s(str);
ct(str);
system("pause");
}
void ct(char* p)
{ //创建大写字母、小写字母、空格、数字、其它的计数整数
int cl, sl,bs, num, ot;
cl = 0,sl= 0,bs=0, num = 0, ot = 0;
while (*p != '\0')
{
if (*p >= 'A' && *p <= 'Z') cl++;
else if (*p >= 'a' && *p <= 'z') sl++;
else if (*p == ' ') bs++;
else if (*p >= '0' && *p <= '9') num++;
else ot++;
p++;
}
cout << "cl=" << cl << "\t"
<< "sl=" << sl << "\t"
<< "bs=" << bs << "\t"
<< "num=" << num << "\t"
<< "ot=" << ot << "\t";
}