c语言编程。从标准输入设备上输入一个字符串,分别统计其中每个数字,空格,字母及其他字符出现的次数。

求解

代码如下:

#include <stdio.h>

int main(){

char stringss[1024];

int i,num[4]={0};

int j = 0;

char c;

while((c=getchar()) !='\n'){

stringss[j] = c;

j++;

}

for(i=0;i<j;i++)//统计字符串,遇到'\0'结束

{

if(stringss[i]>='0'&&stringss[i]<='9')//统计数字个数

num[0]++;

else if(stringss[i]==' ')//统计空格

num[1]++;

else if(stringss[i]>='A'&&stringss[i]<='Z'||stringss[i]>='a'&&stringss[i]<='z')//统计字符

num[2]++;

else

num[3]++;//其他

}

printf("出现的数字%d个,出现的空格%d个,出现的字母%d个,其他字符%d个\n",num[0],num[1],num[2],num[3]);

}

程序运行结果如下:

扩展资料

C语言中对字符串的统计:可以通过循环数组的方式去一个一个的比较字符,然后进行统计。首先可以通过循环的方式读取每个数字,直到读到换行符“\n”,就结束读取,把读取的字符存到数组中,同时记录下字符的长度,然后进行循环统计,打印出现个数就完成了。

参考资料:百度百科-C语言

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-11-28
//.输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。
#include"stdio.h"
void scan(char *a);
int word=0,space=0,num=0,nother=0;
void main()
{
printf("输入一行字符:");
char a[40];
gets(a);
scan(a);
printf("字母%d,\n空格%d,\n数字%d,\n其他%d\n\n",word,space,num,nother);
}
void scan(char *a)
{
int i;
for(i=0;a[i]!='\0';i++)
{
if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z'))word++;
else if(a[i]==32)space++;
else if(a[i]>='0'&&a[i]<='9')num++;
else nother++;
}
}本回答被提问者和网友采纳
第2个回答  2013-05-21
只需对这个字符串进行遍历-------第一步
使用if -----else if----else 条件判断你要得条件就行了!
第3个回答  2013-05-21
#include <stdio.h>
void main()
{
char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\n");
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
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,space,digit,others);
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网