0050 输入字符串,字符串以‘*#*’结尾,判断每个字符串中0-9数值各有多少个?


问题描述:

  输入字符串,字符串以‘*#*’结尾,判断每个字符串中0-9数值各有多少个?

输入样例:

  输入“9jss7h21h21H326tu2sw378*#*”,

输出样例:

  输出"0:0,1:2,2:4,3:2,4:0,5:0,6:1,7:2,8:0,9:1",输出格式不限制

代码展示:

 1 #include
 2 int main(){
 3     char str[100];
 4     int count = 0;    //统计读入的字符个数 
 5     int num[10];
 6     int i;            //循环变量 
 7     for(i=0;i<10;i++){
 8         num[i] = 0;
 9     }    
10     while(1){        //循环读入单个字符,判断是否结束 
11         scanf("%c",&str[count]);
12         count++;
13         if(count>=3){
14             if(str[count-1] == '*' && str[count-2] =='#' && str[count-3] == '*'){
15                 break;
16             }
17         }
18     }
19     for(i=0; i//统计各字符的个数 
20         if((str[i]>='0') && str[i]<='9'){
21             num[str[i]-'0']++;
22         }
23     }
24     for(i=0; i<10; i++){        //输出各字符的个数 
25         printf("%d:%d\n",i,num[i]);
26     }
27     return 0;
28 } 

运行截图:

相关