Convert string to int array

When programming in C, I was stuck in the following code fragment:

When converting from a char array inputto an integer array digit, only ANSI code is converted to an array of numbers.

How can I make sure that the correct integer value is given for this array?

This is the corresponding code:

int main(void) {
    int x = 0;
    int y = 0 
    char input[12] = {0};
    int digit[12] = {0};

    scanf("%s", &input[0]);
    fflush(stdin);

    while (input[y] != '\0') {
        if (isdigit(input[y])) {
            digit[x++] = input[y++];
            count++;
        }
        else y++;
    }
}
+2
source share
5 answers
scanf("%s", &string[0]);  

read into an array of characters input. you are reading some other variable.

scanf("%s",input);

or even better

fgets(input,sizeof input, stdin );

  digit[x++] = input[y++]-'0';   
   // substract `'0'` from your character and assign to digit array.

For example, if input[i]=='3'==> '3'-'0'leads to an integer digit3

+2
source

strtol sscanf . .

- :

char *c = "12 23 45 9";
int i[5];
sscanf(inputstring, "%d %d %d %d %d", &i[0], &i[1], &i[2], &i[3], &i[4]);

atoi(), :

int main(int argc,char *argv[]){
    char y[10] = "0123456789";
    char x[3];
    int i;

    x[0] = y[8];
    x[1] = y[9];
    x[2] = '\0';

    i=atoi(x);
}
+1

ASCII 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. , , , :

if (isdigit(input[y])) {
    digit[x++] = input[y++] - '0';

"0", "0" - "0" = 0, "1", "1" - "0" = 1 ..

. 0x3 ASCII Quickref card.

0

, , int, , u, .

input[12] : 12451'\0' <--- size of array of input is 12

: -

digit[12] : 124510000000

, ,

int main(void) {
  int x = 0;
  int y = 0 
  char input[12] = {0};


  scanf("%s", &input[0]);
  int ch_len = strlen(input)/sizeof(char);
  int digit[ch_len];    
  fflush(stdin);

  while (input[y] != '\0') {
      if (isdigit(input[y])) {
          digit[x++] = input[y++]-'0';
          count++;
      }
      else y++;
  }
}
0

, .

#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
0
source

All Articles