亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要編寫一函數,由實參傳來一個字符串,統計此字符串中字母、數字、空格和其它字符的個數,怎么辦!

需要編寫一函數,由實參傳來一個字符串,統計此字符串中字母、數字、空格和其它字符的個數,怎么辦!

至尊寶的傳說 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 關注
  • 834 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號