为了账号安全,请及时绑定邮箱和手机立即绑定

需要编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,怎么办!

需要编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,怎么办!

至尊宝的传说 2021-12-02 19:15:17
编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串,输出上述结果。程序编写时最好把存放字母、数字、空格和其它字符的变量定义为全程变量,这样在函数中就不必定义了。用其它方法也可以。#include "stdio.h"int letter=0,number=0,space=0,other=0,i;main(){char a[30];gets(a);fff(a);printf("letter=%d,number=%d,space=%d,other=%d",letter,number,space,other);}fff(b)char b[30];{for (i=0;b[i]!='\0';i++){if(65<=b[i]<=90||97<=b[i]<=122) letter++;else if(48<=b[i]<=57) number++;else if(b[i]==32) space++;else other++;}}帮我看看错哪了,输什么都算成letter
查看完整描述

2 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

不能使用连续的不等式的。

#include "stdio.h"

int letter=0,number=0,space=0,other=0,i;

main()

{

char a[30];

gets(a);

fff(a);

printf("letter=%d,number=%d,space=%d,other=%d\n",letter,number,space,other);

system("pause");

}

fff(b)

char b[30];

{

for (i=0;b[i]!='\0';i++)

{if(65<=b[i]&&b[i]<=90||97<=b[i]&&b[i]<=122) letter++;

else if(48<=b[i]&&b[i]<=57) number++;

else if(b[i]==32) space++;

else other++;}

}



查看完整回答
反对 回复 2021-12-07
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

#include<stdio.h>
#include<string.h>
void inputSTr(char *args){
char temp[100]={0},str[100];
int b=0,c=0,d=0,e=0,i;
for(i=0;i<strlen(args);i++){
strncpy(temp, args+i, 1);
str[i] = args[i];
if(((int)str[i]>=97&&(int)str[i]<=122)||((int)str[i]>=65&&(int)str[i]<=90))
{
b+=1;
}
else if((int)str[i]==32)
{
c+=1;
}
else if(((int)str[i]>=48)&&((int)str[i]<=58))
{
d+=1;
}
else
{
e+=1;
}
}
printf("这个字符串中:字母有%d个,数字有%d个,空格有%d个,其它字符有%d个。\n",b,d,c,e);
}
void main(){
char *x,input[100];
printf("请输入一个字符串:");
gets(input);
x=input;
inputSTr(x);
}



查看完整回答
反对 回复 2021-12-07
  • 2 回答
  • 0 关注
  • 660 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信