c语言统计字符数
发布时间: 2025-06-21 15:07:40
❶ c语言 统计字符个数
要统计英文字母,空格,数字和其他字符的个数,代码如下:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char c;
int letters=0;
int space=0;
int digit=0;
int other=0;
printf("请输入一行字符:>");
while((c=getchar())!='\n')
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
{
letters++;
}
else if(''==c)
{
space++;
}
else if(c>='0'&&c<='9')
{
digit++;
}
else
{
other++;
}
}
printf("字母的个数:>%d\n空格的个数:>%d\
\n数字的个数:>%d\n其他字符的个数:>%d\n",\
letters,space,digit,other);
system("pause");
return 0;
}
❷ 用c语言编程,对输入的一行字符进行统计分析,要求统计并输出出现的数字字符及其个数(用冒号分隔)
C代码和运行结果如图:
输出符合样例,望采纳~
附源码:
#include <stdio.h>
int main() {
char s[100]; // 输入不超过100字符
int i = 0, cnt[10] = {0}; // 0-9每个数字字符个数统计
fgets(s, 100, stdin); // 读入一行字符,包括换行符' '
while (s[i] != ' ') { // 遇到换行即到结尾,改成空字符'