如何用c语言输入一个字符串,把里面连续的数字依次存放到一个数组中

2024-11-30 17:31:21
推荐回答(4个)
回答1:

void main()
{
char a[80],*p=a;
int b[80]={0},i=0,j=0,x=0; //!!!!
printf("请输入一串字符\n");
gets(a);
for(p=a;(*p)!='\0';p++)
{
if(((*p)>='0')&&((*p)<='9')) //!!!!
{
if(x==0)
{
b[i]=(*p)-48;
i++;
}
else
b[i-1]=b[i-1]*10+(*p)-48;
x=1;
}
else
x=0;
}
for(j=0;b[j]!=0;j++) //!!!!
printf("%d ",b[j]);
printf("共有%d个",j);
printf("\n");

}

回答2:

#include
#include

int main(int argc, char* argv[])
{
const char str[256] = "sd348ghi 35qeaio843ud843";
char sNumbers[32][32];

int i = 0, j = 0, k = 0;
int len = strlen(str);
int bNumber = 0;

while (i < len)
{
bNumber = 0;
while ((str[i] < '9') && (str[i] > '0'))
{
bNumber = 1;
sNumbers[j][k++] = str[i++];
}
if (bNumber)
{
sNumbers[j][k++] = '\0';
j++;
bNumber = 0;
}
i++;
k = 0;
}
printf("Count: %d\n", j);
for (i = 0; i printf("%s\n", sNumbers[i]);

getch();
return 0;
}

回答3:

#include
#include

void main()
{
char str[1024];
char *p;
int a[64];
int n = 0;
int i;

printf("please input: ");
gets(str);
for(p = str; *p != '\0'; p++)
{
if (*p >= '0' && *p <= '9')
{
a[n++] = atoi(p);
for(++p; *p >= '0' && *p <= '9'; p++);
}
}
printf("total %d integer(s).\n", n);
for(i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}
}

回答4:

定义一个足够的数组,循环字符数组,遇到数字就放到新的数组中
~~~~~~~~~~