简体   繁体   English

在 C 中扫描 n 个没有空格的数字

[英]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.假设在一行中输入 n 个数字,没有任何空格,条件是这些数字服从它们位于 1 和 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假设 n 是 6 ,然后让输入像 "239435" 那么如果我有一个数组来存储这些数字,那么我应该得到

    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?我可以通过使用array[0]=(input/10^n)然后下一个数字来获得上述结果,但有没有更简单的方法来做到这一点?

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.只需为每个数字减去0的 ASCII 码,就可以得到它的值。

 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:假设0不是有效输入并且只允许 1-10 之间的数字:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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