在主函数中输入一个字符串,编写一函数通过在主函数中调用它来统计并输出该字符串中字母和数字的个数。(将字符串放入一个字符数组中,并且定义一个字符指针指向字符数组首元素地址,然后利用字符指针的移动逐个字符来判断)。
供参考:
#include <iostream>
#include <cctype>
using namespace std;
void Count_char(char* s)
{
char* p = s;
int cnt_i = 0, cnt_n = 0;
while (*p) {
if (isalpha(*p))
cnt_i++;
else if (isdigit(*p))
cnt_n++;
p++;
}
cout << "英文字母:" << cnt_i << " " << "数字:" << cnt_n;
}
int main()
{
char s[4069];
cin.get(s,4069);
Count_char(s);
return 0;
}