这是我解析所有可以与其他字符混合的数字的解决方案。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
uint8_t atoia(char *src, int *dst, int len){
// This function convert char array with digits into ints array.
// In addition return amount of digits that was able to find in *src.
// If more digits is in *src then max size of *dst, then zero is returned and
// *dst is zeroed.
uint8_t k=0;
uint8_t x=0;
dst[x] = 0;
while(*src++){
if (*src >= '0' && *src <= '9'){
if (x > len-1){
memset(dst, 0, len*sizeof(uint8_t));
return 0;
}
dst[x] = dst[x]*10 + *src - '0';
k = 1;
} else if (k>0){
x++;
dst[x] = 0;
k = 0;
}
}
return x;
}
int main(void){
printf("Hello :)\n");
char *buf = "This is mixed string 3 0 12 233 18 100 321 and 231 123345";
int k=0;
int dst[9]={0};
k = atoia(buf, dst, 9);
while(k--){
printf("Number: %d: %d\n", k, dst[k]);
}
return 0;
}
结果:
Hello :)
Number: 8: 123345
Number: 7: 231
Number: 6: 321
Number: 5: 100
Number: 4: 18
Number: 3: 233
Number: 2: 12
Number: 1: 0
Number: 0: 3