简体   繁体   中英

scan n numbers without spaces in C

Suppose n numbers are to be input in a single line without any spaces given the condition that these numbers are subject to the condition that they lie between 1 and 10.

Say n is 6 , then let the input be like "239435" then if I have an array in which I am storing these numbers then I should get

    array[0]=2
    array[1]=3
    array[2]=9
    array[3]=4  
    array[4]=3

I can get the above result by using array[0]=(input/10^n) and then the next digit but is there a simpler way to do it?

You can use a string to take the input and then check each position and extact them and store in an array. You need to check for the numeric value in each location explicitly, as you are accepting the input as a string. For integers taken input as string, there's no gurantee that the input is pure numeric and if it is not, things can go wild.

check this code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
        char ipstring[64];
        int arr[64];
        int count, len = 0;
        printf("Enter the numbersi[not more than 64 numbers]\n");
        scanf("%s", ipstring);
        len = strlen(ipstring);
        for (count = 0; count < len ; count++)
        {
                if (('0'<= ipstring[count]) && (ipstring[count] <= '9'))
                {
                        arr[count] = ipstring[count] - '0';

                }
                else
                {
                        printf("Invalid input detectde in position %d of %s\n", count+1, ipstring );
                        exit(-1);
                }
        }
        //display
        for (count = 0; count < len ; count++)
        {
                printf("arr[%d] = %d\n", count, arr[count]);
        }
        return 0;
}

Just subtract the ASCII code of 0 for each digit and you get the value of it.

 char *s =  "239435"
 int l = strlen(s);
 int *array = malloc(sizeof(int)*l);
 int i;
 for(i = 0; i < l; i++)
      array[i] = s[i]-'0';

update

Assuming that 0 is not a valid input and only numbers between 1-10 are allowed:

 char *s =  "239435"
 int l = strlen(s);
 int *array = malloc(sizeof(int)*l);
 int i = 0;
 while(*s != 0)
 {
      if(!isdigit(*s))
      {
           // error, the user entered something else
      }

      int v = array[i] = *s -'0';

      // If the digit is '0' it should have been '10' and the previous number 
      // has to be adjusted, as it would be '1'. The '0' characater is skipped.
      if(v == 0)
      {
           if(i == 0)
           {
               // Error, first digit was '0'
           }


           // Check if an input was something like '23407'
           if(array[i-1] != 1)
           {
               // Error, invalid number
           }
           array[i-1] = 10;
      }
      else
          array[i] = v;

     s++;
 }

Eg

int a[6];
printf(">");
scanf("%1d%1d%1d%1d%1d%1d", a,a+1,a+2,a+3,a+4,a+5);
printf("%d,%d,%d,%d,%d,%d\n", a[0],a[1],a[2],a[3],a[4],a[5]);

result:

>239435
2,3,9,4,3,5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM