题解 | #使用字符函数统计字符串中各类型字符的个数#
使用字符函数统计字符串中各类型字符的个数
https://www.nowcoder.com/practice/31bdbc70188f48e995fa3cbef36613c8
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
gets(str);
int whitespace = 0;
int digits = 0;
int chars = 0;
int others = 0;
for (int i = 0; str[i]!='\0'; i++) { //遍历字符串
if ((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) //判断是否是字母
chars++;
else if (str[i]>='0' && str[i]<='9') //判断是否是数字
digits++;
else if (str[i]==' ') //判断是否是空格
whitespace++;
else
others++;
}
printf("chars : %d whitespace : %d digits : %d soters : %d ",chars,whitespace,digits,others);
return 0;
}
SHEIN希音公司福利 350人发布
查看15道真题和解析