【C语言】如何将字符串内的数字赋值给数组?

例如,现有字符串str,值为“18 9 6 33 28 7”,另有int型数组A[6],如何将str中的数字分别赋值给数组中的每个元素?即,使A[0]=18,A[1]=9,A[2]=6,以此类推。十分感谢!

int index = 0;
for(int i == 0; i < strlen(str); i++)
{
if(i == 0)
{
sscanf(str,"%d",&A[index]);
index++;
}
if (str+i == '\0')
{
sscanf(str+i+1,"%d",&A[index]);
index++;
}
}
这样应该可以
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-21
int i;
for(i=0;i<6;i++)
{
sscanf(str,"%d",&A[i]);
}
第2个回答  2012-08-21
#include<stdio.h>
#include<string.h>
main()
{
char str[81],*p;
int i,j,num,A[81]={0};
gets(str);
p=str;
for(i=0;*p!='\0';p++){
if(*p!=' '){
num=*p-65;
A[i]=A[i]*10+num;
}
else
i++;
}
for(j=0;j<=i;j++)
printf("%d",&A[j]);
}
相似回答