2

While programming in C I got stuck at the following code-snippet:

While converting from char array input to integer array digit, only the ANSI code is converted to the digit array.

How can I make sure the correct integer-value is given to this array ?

This is the relevant 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++;
    }
}

5 Answers 5

2
scanf("%s", &string[0]);  

read into input character array. you are reading into some other variable.

scanf("%s",input);

or even best

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' will result in integer digit 3

Sign up to request clarification or add additional context in comments.

1 Comment

Ah thank you! Yeah I changed all the variables, because all of them were in Dutch! It is working now, I have added the -'0' add the end, thanks for the valuable feedback!
1

I would suggest you to use strtol or sscanf in a loop. That would make things easier for you.

Something like this:

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]);

Sample example using atoi() which another option:

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 Comment

Thank you, I will definitely check that method out!
0

In ASCII the number symbols appear in the sequence as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. So all you have to do is this:

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

If the digit is '0', '0' - '0' = 0, if the digit is '1', '1' - '0' = 1, and so on.

See column 0x3 on the Wikipedia ASCII Quickref card.

1 Comment

Thank you, will keep this one in mind, very valuable feedback!
0

You have to do few improve that we have to make sure array of int digit, array size should be same as how much input u type for eg.

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

Output :-

digit[12] : 124510000000

because remaining array size is unused so we have to make sure how input much u type it will be size of digit array

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++;
  }
}

Comments

0

This is my solution to parse all digits that can be mixed with other characters.

#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;
}

Result:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.