题解 | #使用字符函数统计字符串中各类型字符的个数#

使用字符函数统计字符串中各类型字符的个数

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;

}

全部评论
点赞 回复 分享
发布于 2024-03-06 10:45 山东

相关推荐

溱元:前端每年固定死几次,看两集广告就复活了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务