C语言编程题

2024-10-31 17:26:12
推荐回答(4个)
回答1:

/*
asd123rt456fg789
*/

#include
#include

void main()
{
char str[1000];
int num[20][20],sum=0,q=1;
int i,j,p=0,len=0,numi=0,numj[20],count;
gets(str);
len=strlen(str);

for(i=0;i<20;i++)
{
numj[i]=0;
}

count=0;

for(i=0;i {
if(str[i]>='0'&& str[i]<='9')
{
num[numi][numj[count]]=str[i]-'0';
printf("num[%d][%d]=%d\n",numi,numj,num[numi][numj[count]]);
p=1;
numj[count]++;
}
else
{
if(p==1)
{ numi++;
count++;
p=0;
}
}
}

sum=0;

for(i=0;i<=numi;i++)
{
for(j=numj[i]-1;j>=0;j--)
{
sum+=(q*num[i][j]);
q*=10;
}
printf("%d\n",sum);
sum=0;
q=1;
}

}

回答2:

上面的思路很正确。但不足的是万一结束条件不是'\0'就会出问题。保险起见,建议先获取输入的字符串个数。用字符串个数来做结束条件。

回答3:

void printnum(char* buf)
{
int i=0;
int printed=0;
while(buf[i] != '\0')
{
if (buf[i] >= '0' && buf[i] <= '9')
{
printf("%c", buf[i]);
printed = 1;
}
else
{
if (printed)
{
printf("\n");
printed = 0;
}
}
i++;
}
}

回答4:

#include
main()
{
char str[]"asd123rt456fg789";
int i;
for (i=0;iif(str[i]>='0'&&str[i]<='9');
print("%d\n");
}