本文共 714 字,大约阅读时间需要 2 分钟。
#include int letter_count=0,num_count=0,other_count=0;//用于统计字符串中字母、数字和其他字符的数量void char_count(char *str){ int i=0; while(*(str+i) != '\0'){ char c = *(str+i); if( c >= '0' && c <= '9') num_count++; else if( (c >= 'A' && c <='Z') || (c >= 'a' && c <= 'z')) letter_count++; else other_count++; i++; }}int main() { char str[50]; gets(str); char_count(str); printf("字母数为:%d\t数字数:%d\t其他字符数为:%d\n",letter_count,num_count,other_count); system("pause"); return EXIT_SUCCESS;}
注:以上代码描述了一个用于统计字符类型的程序,适用于处理字符串中的不同字符分类统计场景。设置三个计数器分别记录字母、数字和其他字符的数量,可根据实际需求进行修改和扩展,支持多种字符分类识别功能。
转载地址:http://yizhz.baihongyu.com/