解题思路:
利用cctype字符函数库解决问题
cctype库内函数
isalnum(c); 如果参数是字母或数字,函数就返回true,否则返回false
isalpha(c); 如果参数是字母,函数返回true, 否则返回false
islower(c); 如果参数是小写字母,函数返回true
isupper(c); 如果参数是大写字母,函数返回true
isdigit(c); 如果参数是数字,函数返回true
isxdigit(c); 如果参数是十六进制数字,即0 ~ 9,a ~ f或A ~ F,函数返回true
ispunct(c); 如果参数是标点符号,函数返回true
iscntrl(c); 如果参数是控制字符,函数返回true
isgraph(c); 如果参数除空格之外的打印字符,函数返回true
isprint(c); 如果参数是打印字符(包括空格' '),函数返回true
注意事项:
循环的判断条件,头文件#include<cctype>
参考代码:
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char ch;
int digits =0, chars=0 ,whitespace=0 ,others=0 ;
cin.get(ch);
while (ch!='\n') {
if (isalpha(ch)) {
chars++;
}
else if (isdigit(ch)) {
digits++;
}
else if (isspace(ch)) {
whitespace++;
}
else {
others++;
}
cin.get(ch);
}
cout << chars << " " << digits << " " << whitespace << " " << others;
return 0;
}
0.0分
3 人评分
【亲和数】 (C语言代码)浏览:536 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:664 |
十->二进制转换 (C语言代码)浏览:1291 |
C语言考试练习题_排列 (C语言代码)浏览:719 |
WU-输出正反三角形 (C++代码)浏览:1018 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:665 |
矩阵的对角线之和 (C语言代码)浏览:1343 |
【计算直线的交点数】 (C语言代码)浏览:916 |
简单的a+b (C语言代码)浏览:504 |
C语言程序设计教程(第三版)课后习题10.5 (C语言代码)浏览:546 |