有两种判断字符是何种类型的方式。
1) 直接测试字符是何种类型:check1
2) 利用字符分类函数:check2
参考代码如下:
#include <stdio.h> #include <string.h> #include <stdlib.h> void check1(char *str) { int len = strlen(str); int abc = 0; int num = 0; int space = 0; int other = 0; int i; for (i = 0; i < len; i++) { if (str[i] <= 'z' && str[i] >= 'a') abc++; else if (str[i] <= 'Z' && str[i] >= 'A') abc++; else if (str[i] <= '9' && str[i] >= '0') num++; else if (str[i] == ' ') space++; else other++; } printf("%d %d %d %d\n", abc, num, space, other); } void check2(char *str) { int len = strlen(str); int abc = 0; int num = 0; int space = 0; int other = 0; int i; for (i = 0; i < len; i++) { if (isalpha(str[i])) abc++; else if (isdigit(str[i])) num++; else if (isspace(str[i])) space++; else other++; } printf("%d %d %d %d\n", abc, num, space, other); } int main() { char str[100]; scanf("%[^\n]", str); check1(str); check2(str); return 0; }
0.0分
0 人评分
【出圈】 (C语言代码)浏览:554 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:749 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:377 |
输出正反三角形 (C语言代码)浏览:787 |
C语言程序设计教程(第三版)课后习题5.7 (Java代码)浏览:889 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:507 |
1126题解浏览:585 |
数列排序 (C语言代码)浏览:608 |
1202题解浏览:622 |
C语言训练-斐波纳契数列 (C语言代码)浏览:505 |