一个C语言程序关于输入一行字符分别统计出其中字母、空格、数字和其他字符的个数

一个C语言程序关于输入一行字符分别统计出其中字母、空格、数字和其他字符的个数#include<stdio.h>
int letter,digit,space,others;
void main()
{
void count(char[]);
char text[80];
printf("Please enter string:\n");
gets(text);
printf("string:\n");
puts(text);
letter=0;
digit=0;
space=0;
others=0;
count(text); printf("letter:%d,digit:%d,space:%d,others:%d\n",letter,digit,space,others);
}
void count(char str[])
{
int i;
for(i=0;str[i]!='\0';i++)
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
letter++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==32)
space++;
else
others++;
}
那个str[i]是什么意思,还有为什么str[i]==32时space++

str[i]是指数组的位置,将一个字符串转成char类型的字符数组,
然后,for循环遍历该数组的每一个字符。
str[i]是指数组的位置,i为前面for里面的一个自增变量。
str[i]=32,你可以查看acsii表,编号为32的就是空格。因为一行英文字母中有可能有空格字符。
char类型的数据,本身可以转成int型 。互转方式就是通过ascii表进行互转。
space自变量指的是空格字符。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-01-15
str[i]是取字符串中第i个字符, str[i]==32判断i字符是否为空格, 32为空格符的asc码
第2个回答  2020-03-09

C语言经典例子之统计英文、字母、空格及数字个数

第3个回答  2020-03-24
相似回答