【问题标题】:convert array of characters to array of integers in C将字符数组转换为C中的整数数组
【发布时间】:2011-08-13 09:41:05
【问题描述】:

我正在向 C 程序传递一个参数:

程序名称 1234

int main (int argc, char *argv[]) {

    int     length_of_input = 0;    
    char*   input           = argv[1];


    while(input[length_of_input]) {
        //convert input from array of char to int
        length_of_input++;
    }
}

我希望能够将传入函数的参数的每个数字分别用作整数。 atoi(input[]) 抛出编译时错误。

此代码无法编译:

while(input[length_of_input]) {
    int temp = atoi(input[length_of_input]);
    printf("char %i: %i\n", length_of_input, temp);
    length_of_input++;
}

【问题讨论】:

  • 请更准确地说你想要什么。你期望一个包含 {1,2,3,4} 的 int[] 吗?另外,这闻起来像作业,所以标记它
  • 确定它只是更大的家庭作业的一小部分,正如您可能已经从我今天提出的其他问题中看到的那样。但我认为它不需要这样标记,因为这是一个微不足道的语言问题。

标签: c char int atoi


【解决方案1】:
int i;
for (i = 0; input[i] != 0; i++){
    output[i] = input[i] - '0';
}

【讨论】:

  • 你忘了空终止每个字符......你最终会得到一个包含 {1234, 234, 34, 4} 的整数数组。
  • output[i] = input[i] - '0';
  • @Lou 和@Conrad 你是对的,如果我们要转换单个字符,就没有理由使用 atoi(如果它不是以 null 结尾的,我们会遇到问题 >_
  • == 应该是 != 在循环中。
  • @Alok 为什么是的,是的。这就是我在不编译的情况下输入代码所得到的!谢谢=)
【解决方案2】:

你可以用 isdigit() 测试参数是否为数字

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

并使用 atoi 函数。

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

并且使用时要小心

char*   input           = argv[1];

将字符串从 argv 复制到输入(在使用 malloc 之后),这样会更好。

【讨论】:

    【解决方案3】:

    既然这是家庭作业,你也可以做

    output[i] = input[i] - '0';
    

    但请注意input[i] 实际上是一个数字(即它在'0''9' 之间)!

    【讨论】:

      【解决方案4】:

      首先,您必须检查需要为整数数组分配多少空间。这可以通过strlen() 函数或遍历字符串并检查找到多少有效字符来完成。然后您必须遍历字符串并将每个(有效)字符转换为等效的整数。在这里很难使用atoi()scanf() 系列函数,因为它们除了数组作为输入。更好的解决方案是编写自己的小转换器函数或 sn-p 进行转换。

      这是一个将字符串转换为整数数组的小示例应用程序。如果字符不是有效的十进制数字,则将-1 放入数组中。

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      int main(int argc, char *argv[])
      {
          int length, i;
          int *array;
          char *input = argv[1];
      
          /* check if there is input */
          if(input == NULL) return EXIT_FAILURE;
      
          /* check the length of the input */
          length = strlen(input);
          if(length < 1) return EXIT_FAILURE;
      
          /* allocate space for the int array */
          array = malloc(length * sizeof *array);
          if(array == NULL) return EXIT_FAILURE;
      
          /* convert string to integer array */
          for(i = 0; i < length; ++i) {
              if(input[i] >= '0' && input[i] <= '9')
                  array[i] = input[i] - '0';
              else
                  array[i] = -1; /* not a number */
          }
      
          /* print results */
          for(i = 0; i < length; ++i)
              printf("%d\n", array[i]);
      
          /* free the allocated memory */
          free(array);
      
          return EXIT_SUCCESS;
      }
      

      同时检查这些问题:

      【讨论】:

        猜你喜欢
        • 2018-10-07
        • 2016-04-15
        • 2018-05-18
        • 1970-01-01
        • 2013-11-11
        • 1970-01-01
        • 2018-04-25
        • 2020-07-23
        相关资源
        最近更新 更多