【问题标题】:Converting a string into an int array将字符串转换为 int 数组
【发布时间】:2013-10-19 16:55:16
【问题描述】:

在 C 中编程时,我遇到了以下代码-sn-p:

从字符数组input转换为整数数组digit时,只有ANSI代码转换为数字数组。

如何确保为这个数组提供正确的整数值?

这是相关代码:

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

【问题讨论】:

    标签: c arrays char int


    【解决方案1】:
    scanf("%s", &string[0]);  
    

    读入input 字符数组。您正在读取其他变量。

    scanf("%s",input);
    

    甚至是最好的

    fgets(input,sizeof input, stdin );
    

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

    例如,如果input[i]=='3' ==> '3'-'0' 将产生整数位 3

    【讨论】:

    • 啊,谢谢!是的,我更改了所有变量,因为它们都是荷兰语!它现在正在工作,我已经添加了 -'0' 添加结束,感谢您的宝贵反馈!
    【解决方案2】:

    我建议您在循环中使用strtolsscanf。这会让你的事情变得更容易。

    类似这样的:

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

    【讨论】:

    • 谢谢你,我一定会检查那个方法的!
    【解决方案3】:

    在 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,依此类推。

    请参阅Wikipedia ASCII Quickref card 上的第 0x3 列。

    【讨论】:

    • 谢谢,会记住这一点,非常有价值的反馈!
    【解决方案4】:

    你必须做一些改进,我们必须确保 int 数字数组,数组大小应该与你输入的输入量相同。

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

    【讨论】:

      【解决方案5】:

      这是我解析所有可以与其他字符混合的数字的解决方案。

      #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
      

      【讨论】:

        猜你喜欢
        • 2021-12-21
        • 2016-08-14
        • 2012-03-26
        • 1970-01-01
        • 2020-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        相关资源
        最近更新 更多