我应该写一个函数不使用其他库,但是从用户获取字符串(最多100个字符)。字符串将是这种形式:A,B,C,d,X,Y,Z等...从我应该在逗号之间仅提取第一整数到不同的阵列。 因此,例如,比方说,我得到这个作为输入:ABCD,32,23.5,4,6,hf3,45g,2个 新数组中的数字应该是:32,4,6,2,因为这是第一次你可以在输入中看到4个数字。如果没有发现号默认值为0如何检查字符串中某个位置的数字并在c中提取其值?
这是我到目前为止一直在做,但不知何故,似乎并不是正确的, 想法是seperately检查每个字符,并除非看到逗号并且字符在'0'和'9'的ASCII值之间进行求和。如果在逗号前面出现了不同的字符,那么“跳过”将得到值1,这样该函数将继续寻找另一个数字。
感谢您的帮助。
int getParameters()
{
char input[100];
int parameters[4]={0};
int indexInput=0, indexParameters;
int charValue=0;
int skip=1;
scanf("%s", input);
for (indexParameters=0; indexParameters<4;skip=0)
{
if (input[indexInput]=='\0')
break;
else
{
for (;input[indexInput]!=','; ++indexInput)
{
printf("%c\n", input[indexInput]);
if(input[indexInput]=='\0')
break;
else if (input[indexInput]<'9' &&
input[indexInput]>'0')
{
charValue=input[indexInput]-'0';
parameters[indexParameters]*=10;
parameters[indexParameters]+=charValue;
}
else
{
skip=1;
}
}
}
indexInput++;
if (input[indexInput]==",")
skip==1;
if (skip==1)
{
parameters[indexParameters]=0;
}
else
indexParameters++;
}
return 0;
}
_but不知它似乎并没有被right._究竟如何?预期的输出是32,4,6,2。你得到的输出是什么? – 2014-12-07 18:34:30
线'如果(输入[indexInput] == “”),则跳过== 1;'当然应该是'如果(输入[indexInput] == '')跳过= 1;':您分配1〜'跳过'并且测试一个cahacter(用单引号)。 – 2014-12-07 18:35:22
另外,您从数字中排除0和9。 – 2014-12-07 18:37:24