C程序随机输入一个字符串,统计该串中每个数字字符(0,1,2,3,4,5,6,7, 8,9)出现的次 5
C程序随机输入一个字符串,统计该串中每个数字字符(0,1,2,3,4,5,6,7,8,9)出现的次数。考虑:(1)用num[0]来统计字符0的个数,用num[1]来统计字...
C程序随机输入一个字符串,统计该串中每个数字字符(0,1,2,3,4,5,6,7, 8,9)出现的次数。
考虑:
(1)用num[0]来统计字符0的个数,用num[1]来统计字符1的个数......用num[9]来统计字符9的个数。
(2)如何实现一个字符串的输入。 展开
考虑:
(1)用num[0]来统计字符0的个数,用num[1]来统计字符1的个数......用num[9]来统计字符9的个数。
(2)如何实现一个字符串的输入。 展开
展开全部
#include <stdio.h>
typedef struct
{
unsigned int num0;
unsigned int num1;
unsigned int num2;
unsigned int num3;
unsigned int num4;
unsigned int num5;
unsigned int num6;
unsigned int num7;
unsigned int num8;
unsigned int num9;
}NUMCOUNT;
NUMCOUNT NumCount;
int main(void)
{
char buf[100];
memset(buf,0,100);
mmeset(&NumCount.num0,0,sizeof(NumCount));
printf("请输入一个字符串:\n");
scanf("%s",buf);
for(int i=0;i<strlen(buf);i++)
{
switch(buf[i]){
case 0:
NumCount.num0++;
break;
case 1:
NumCount.num0++;
break;
case 2:
NumCount.num0++;
break;
case 3:
NumCount.num0++;
break;
case 4:
NumCount.num0++;
break;
case 5:
NumCount.num5++;
break;
case 6:
NumCount.num6++;
break;
case 7:
NumCount.num7++;
break;
case 8:
NumCount.num8++;
break;
case 9:
NumCount.num9++;
break;
default:
break;
}
}
printf("0的个数:%d\n",NumCount.num0);
printf("1的个数:%d\n",NumCount.num1);
printf("2的个数:%d\n",NumCount.num2);
printf("3的个数:%d\n",NumCount.num3);
printf("4的个数:%d\n",NumCount.num4);
printf("5的个数:%d\n",NumCount.num5);
printf("6的个数:%d\n",NumCount.num6);
printf("7的个数:%d\n",NumCount.num7);
printf("8的个数:%d\n",NumCount.num8);
printf("9的个数:%d\n",NumCount.num9);
}
typedef struct
{
unsigned int num0;
unsigned int num1;
unsigned int num2;
unsigned int num3;
unsigned int num4;
unsigned int num5;
unsigned int num6;
unsigned int num7;
unsigned int num8;
unsigned int num9;
}NUMCOUNT;
NUMCOUNT NumCount;
int main(void)
{
char buf[100];
memset(buf,0,100);
mmeset(&NumCount.num0,0,sizeof(NumCount));
printf("请输入一个字符串:\n");
scanf("%s",buf);
for(int i=0;i<strlen(buf);i++)
{
switch(buf[i]){
case 0:
NumCount.num0++;
break;
case 1:
NumCount.num0++;
break;
case 2:
NumCount.num0++;
break;
case 3:
NumCount.num0++;
break;
case 4:
NumCount.num0++;
break;
case 5:
NumCount.num5++;
break;
case 6:
NumCount.num6++;
break;
case 7:
NumCount.num7++;
break;
case 8:
NumCount.num8++;
break;
case 9:
NumCount.num9++;
break;
default:
break;
}
}
printf("0的个数:%d\n",NumCount.num0);
printf("1的个数:%d\n",NumCount.num1);
printf("2的个数:%d\n",NumCount.num2);
printf("3的个数:%d\n",NumCount.num3);
printf("4的个数:%d\n",NumCount.num4);
printf("5的个数:%d\n",NumCount.num5);
printf("6的个数:%d\n",NumCount.num6);
printf("7的个数:%d\n",NumCount.num7);
printf("8的个数:%d\n",NumCount.num8);
printf("9的个数:%d\n",NumCount.num9);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询