**代码示例如下:**
#include<iostream>
using namespace std;
int main()
{
char str[3][80];
int i, j, upper, lower, digit, space, other;
for (i = 0; i < 3; i++)
{
upper = lower = digit = space = other = 0;
gets_s(str[i]);
for (j = 0; j < 80; j++)
{
if (str[i][j] >= 'A'&& str[i][j] <= 'Z')
upper++;
else if (str[i][j] >= 'a' && str[i][j] <= 'z')
lower++;
else if (str[i][j] >= '0' && str[i][j] <= '9')
digit++;
else if (str[i][j] == ' ')
space++;
else
other++;
}
cout << "upper case:" << upper << endl;
cout << "lower case:" << lower << endl;
cout << "digit :" << digit << endl;
cout << "space :" << space << endl;
cout << "other :" << other << endl;
}
return 0;
}
总结:1.cin输入的字符不接受空字符,而gets可以接受,并且按回车键可以结束输入。
2.用gets为二维数组初始化时,形式如下:gets(str[i]);
调试结果: