【问题标题】:C program: Convert string of chars representing numbers to intsC程序:将表示数字的字符字符串转换为整数
【发布时间】:2018-01-17 17:26:10
【问题描述】:

我正在学习 C,我正在尝试让一个玩具示例工作。我的假用例给出了一个字符串,其中每个 char 代表和 int,循环遍历字符串的每个 char 并将其转换为 int。到目前为止我尝试过的方法没有奏效。

编辑: 原始问题已编辑,现在包含 main(),以允许响应者根据 cmets 中的建议编译和使用 strtol

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

int main(int argc, char **argv) {
  char *charNums = "12345";
  int num, num2, num3, i, n = strlen(charNums);
  char *ptr = charNums;

  for (i = 0; i < n; i++) {
    num = atoi(&charNums[i]);
    num2 = atoi(ptr);
    num3 = strtol(ptr, NULL, 10);
    printf("\ncharNums[%d] = %c (the expected value but as an int not a char)\n num = %d\n num2 = %d\n num3 = %d\n", 
        i, charNums[i], num, num2, num3);
    ptr++;
  }
  return 0;
)

编辑:显示C代码的编译方法以及程序执行

gcc -o soQuestion main.c
./soQuestion
charNums[0] = 1 (the expected value but as an int not a char)
  num = 12345
  num2 = 12345
  num3 = 12345

charNums[1] = 2 (the expected value but as an int not a char)
  num = 2345
  num2 = 2345
  num3 = 2345

charNums[2] = 3 (the expected value but as an int not a char)
  num = 345
  num2 = 345
  num3 = 345

charNums[3] = 4 (the expected value but as an int not a char)
  num = 45
  num2 = 45
  num3 = 45

charNums[4] = 5 (the expected value but as an int not a char)
  num = 5
  num2 = 5
  num3 = 5

感谢任何反馈。

编辑: 该程序的预期结果是将字符字符串“12345”中的每个字符转换为 1 2 3 4 5 的单个整数,以便我可以对它们进行数学运算,例如对它们求和1 + 2 + 3 + 4 + 5 = 15

【问题讨论】:

  • 如果您一次只想要一个字符,循环遍历字符串并从每个字符中减去 '0' 应该可以工作。当然,您必须首先验证您的字符串都是以 10 个字符为基数
  • 究竟是什么没有成功?
  • 在我看来它工作正常
  • 函数:strlen() 返回 size_t 而不是 int
  • 函数:atoi() 获取所有字符,从指定的地址开始,直到到达一个非数字字符,因此代码不是逐个字符而是逐步删除前导数字.此外,不应使用函数atoi()(通常),因为它没有给出成功/失败的指示。建议使用:strtol()

标签: c string char int


【解决方案1】:

您可以在执行subtraction 时将字符视为数字:

int diff = c - '0';  //c is a character. 
//Avoid using hard code values like 48 in place of '0'.
//as they less readable.

那么diff 什么都不是,而是整数值。

【讨论】:

    【解决方案2】:

    你好,我的朋友,我想你想将每个字符转换为整数,所以如果你的要求相同,这里是代码。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int my_atoi(char *str_to_int){
    
        int num = 0;
        for(int rot=0 ; str_to_int[rot] ; rot++){
    
                num = num * 10 + (str_to_int[rot] - 48);
                printf("Char to int is :: %d\n",num);
        }
        return num;
    }
    
    
    int main() {
    
        char *charNums = "12345";
        int get_int;
    
        get_int = my_atoi(charNums);
        printf("In String :: %s In Integer :: %d\n",charNums,get_int);
    }
    

    输出是:

    Char to int is :: 1
    Char to int is :: 12
    Char to int is :: 123
    Char to int is :: 1234
    Char to int is :: 12345
    In String :: 12345 In Integer :: 12345
    

    【讨论】:

      【解决方案3】:

      我的问题的完整答案在这里。感谢@Saurav Sahu 和@yano 带领我朝着正确的方向前进。

      #include <stdio.h>
      #include <stdlib.h>
      
      int main(int argc, char **argv) {
        char *charNums = "12345";
        int num;
      
        for (int i = 0; charNums[i]; i++) {
          num = charNums[i] - '0';
          printf("\ncharNums[%d] = %c", i, charNums[i]);
          printf("\nnum = %d\n", num);
        }
        return 0;
      }
      

      程序的编译和输出。

      ➜  testcompile gcc -o soQuestion main.c
      ➜  testcompile ./soQuestion
      
      charNums[0] = 1
      num = 1
      
      charNums[1] = 2num = 2
      
      charNums[2] = 3
      num = 3
      
      charNums[3] = 4
      num = 4
      
      charNums[4] = 5
      num = 5
      

      【讨论】:

        猜你喜欢
        • 2014-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-04
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多