猿问
下载APP

C语言编程时候的运行问题!求大佬指教!

编程统计输入的字符串中数字字符出现的次数,不知道为啥结果不对!

做一个好码农
浏览 11835回答 42
42回答

qq_一只珂_0

ch[i]的话是字符型,然后放在a[ch[i]]就不对了,改成a[ch[i]-'0']

qq_荒原冰竹_0

#include <stdlib.h>#include <stdio.h>int main(int arg,char * args){ char ch[80]; int count = 0; //用来统计字符出现次数 printf("请输入你的字符.\n"); int i = 0; for(i = 0;i < strlen(ch);i ++) { gets(ch); } while(ch[i] != '\0') { if(ch[i] == 'c')  //此处判断为假设你要查找的字符是c { count++; } } printf("字符c出现的次数为%d次!\n",count); /*以上代码中可能出现的函数使用方法不是很正确,毕竟是用记事本写的代码。如便难以出错请查询函数用法自行改正。。。。。。。 */ return 0; }

慕仙5047159

https://www.imdb.com/LIST/ls530933521/?mode=desktop&ref_=h_tc_dzo https://www.imdb.com/LIST/ls530933521/?mode=desktop&ref_=1=e6=6 https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=d=J0=A https://www.imdb.com/LIST/ls530933521/?mode=desktop&ref_=o_fv=bN_e https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=3_aW=oM_c https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=R_jc_idm https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=1_ay=b https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=2=ju=9 https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=Z_MB=0 https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=R=G4=G https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=b_bl_oxg https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=7_ab=d https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=2=4f=X https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=4_05=8 https://www.imdb.com/LIST/TITLE/ls530933521/?mode=desktop&ref_=6=c7=Q https://www.imdb.com/LIST/ls530933588/?mode=desktop&ref_=e_ih_gfa https://www.imdb.com/LIST/ls530933588/?mode=desktop&ref_=O=wJ=O https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=t=3Y=M https://www.imdb.com/LIST/ls530933588/?mode=desktop&ref_=y_lc=t9_f https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=h_fc=3a_9 https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=5_yw_ydd https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=9_rq=q https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=9=ob=1 https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=3_Ct=9 https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=9=4E=I https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=l_ss_kva https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=6_ux=a https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=0=i7=b https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=O_84=6 https://www.imdb.com/LIST/TITLE/ls530933588/?mode=desktop&ref_=j=YY=q https://www.imdb.com/LIST/ls530933761/?mode=desktop&ref_=x_wi_cyj https://www.imdb.com/LIST/ls530933761/?mode=desktop&ref_=8=Kt=W https://www.imdb.com/LIST/TITLE/ls530933761/?mode=desktop&ref_=e=a5=c https://www.imdb.com/LIST/ls530933761/?mode=desktop&ref_=1_rb=eW_W

慕莱坞8333017

1071187719428751842054115740983064342997674004128282851518818510865108208421955265397873536403708436517434066430730985486747682486308438632081707163243743909373588238361335256920913828985092012804327027258687810351853295357383194217769354093943329279286866424065790208435351932052805899538504847197290670894674182564658210934

慕莱坞8333017

8503291918333616004015607164105594865416984215699998754665101327867681582628798564062847483222113607977313782740116051791368437619555067426903544444678867417602396942352985689935423346978320452009495094845656598595060321209043491708072934096380493164273998540759651896025043525965555541762338847341492174794116290521991099959

慕田峪7504017

写的好烂,a[ch[i]]++是什么鬼,单单就这个数组下标越界就什么都不说了,为什么最后还要用一个for,你是要上天吗?

minmin幕客

buzhidao o 

qq_荒原冰竹_0

第一个for循环,应该改为i++,保证循环次数10次。另外while循环下的if条件判断不对,你要打印的是字符出现的次数,a[10]这个数组其实没必要。定义一个变量统计就可以,打印的语句也有问题。

嗷呜呜呜呜呜呜呜

rrrr

嗷呜呜呜呜呜呜呜

hhh
打开App,查看更多内容
随时随地看视频慕课网APP