C语言:输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数(函数的调用)

2024-11-06 11:25:05
推荐回答(5个)
回答1:

#include
void add(int *a,int *b,int *c,int *d)
{

for (i = 0; i < s.Length; i++)
{
if (s[i] >= '0'&& s[i] <='9')
*a++;
else if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
*b++;
else if(s[i]==' ')
*c++;
else
*d++;
}
}
main()
{
int i,j=0;
int a=0,b=0,c=0,d=0;
string s;
printf("请输入一个字符串:");
while(s[j]!='\n')
{
scanf("%s",&s[j]);
j++;
}
add(&a,&b,&c,&d);
printf("数字的个数是:%d,字母的个数是:%d,空格的个数是:%d,其它字符的个数是:%d", a,b,c,d);
}

回答2:

#include
#include
int main()
{
char s[100]="The furthest distance in the world,1942 is not between life and death.";
int n,nc=0,ns=0,nm=0,no=0;
//nc,ns,nm,no分别表示中英文字符个数,空格个数,数字个数,其他字符个数
int i;
n=strlen(s); //n为字符串长度
for(i=0;iif((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))
nc++;
else if(s[i]==32)
ns++;
else if(s[i]>=48&&s[i]<=57)
nm++;
else
no++;
printf("中英文字符数为:%d\n",nc);
printf("空格数为:%d\n",ns);
printf("数字个数为:%d\n",nm);
printf("其他字符数为:%d\n",no);
return 0;
}

回答3:

#include "stdio.h"
void countof(char *pArry,int *pletter,int *pspace,int *pnumber,int *pother){
char cx;
for(;*pArry;pArry++){
if((cx=*pArry)>64 && cx<91 || cx>96 && cx<123) (*pletter)++;
else if(cx==' ') (*pspace)++;
else if(cx<0x3A && cx>0x2F) (*pnumber)++;
else (*pother)++;
}
}
void main(void){
char Arry[500];
int letter,space,number,other;
letter=space=number=other=0;
printf("Type a long string...\nStr=");
gets(Arry);
countof(Arry,&letter,&space,&number,&other);
printf("Letter=%d\nSpace=%d\nNumber=%d\nOther=%d\n",letter,space,number,other);
}

回答4:

#include
int main()
{
char str[100];
int i=0;
int num=0,ch=0,blank=0,other=0;

gets(str);
while(str[i]!='\0')
{
if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))
ch++;//字母
else if(str[i]>='0' && str[i]<='9')
num++;//数字
else if(str[i]==' ')
blank++;//空格
else
other++;

i++;
}
printf("数字%d个,字母%d个,空格%d个,其他%d个\n",num,ch,blank,other);
return 0;
}

回答5:

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