C语言 将字符串转换为 int 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19468556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:46:01  来源:igfitidea点击:

Converting a string into an int array

carrayscharint

提问by Niels Robben

While programming in C I got stuck at the following code-snippet:

虽然在 CI 中编程陷入了以下代码片段:

While converting from char array inputto integer array digit, only the ANSI code is converted to the digit array.

将字符数组转换input为整数数组时digit,仅将 ANSI 代码转换为数字数组。

How can I make sure the correct integer-value is given to this array ?

如何确保为该数组提供了正确的整数值?

This is the relevant code:

这是相关代码:

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] != '
scanf("%s", &string[0]);  
') { if (isdigit(input[y])) { digit[x++] = input[y++]; count++; } else y++; } }

采纳答案by Gangadhar

scanf("%s",input);

read into inputcharacter array. you are reading into some other variable.

读入input字符数组。您正在阅读其他一些变量。

fgets(input,sizeof input, stdin );

or even best

甚至最好

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


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

For example if input[i]=='3'==> '3'-'0'will result in integer digit 3

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

回答by Rahul Tripathi

I would suggest you to use strtolor sscanfin a loop. That would make things easier for you.

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

Something like this:

像这样的东西:

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] = '
if (isdigit(input[y])) {
    digit[x++] = input[y++] - '0';
'; i=atoi(x); }

Sample example using atoi() which another option:

使用 atoi() 的示例示例,其中另一个选项:

input[12] : 12451'
digit[12] : 124510000000
' <--- size of array of input is 12

回答by kfsone

In ASCII the number symbols appear in the sequence as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. So all you have to do is this:

在 ASCII 中,数字符号在序列中显示为 0、1、2、3、4、5、6、7、8、9。所以你要做的就是:

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] != '
#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;
}
') { if (isdigit(input[y])) { digit[x++] = input[y++]-'0'; count++; } else y++; } }

If the digit is '0', '0' - '0' = 0, if the digit is '1', '1' - '0' = 1, and so on.

如果数字是'0','0' - '0' = 0,如果数字是'1','1' - '0' = 1,依此类推。

See column 0x3 on the Wikipedia ASCII Quickref card.

请参阅维基百科 ASCII Quickref 卡上的 0x3 列。

回答by Vipul Jurel

You have to do few improve that we have to make sure array of int digit, array size should be same as how much input u type for eg.

您必须做一些改进,我们必须确保 int 数字数组,数组大小应与 u 键入的输入数量相同,例如。

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

Output :-

输出 :-

##代码##

because remaining array size is unused so we have to make sure how input much u type it will be size of digit array

因为剩余的数组大小未使用所以我们必须确保你输入的输入量是数字数组的大小

##代码##

回答by MrHetii

This is my solution to parse all digits that can be mixed with other characters.

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

##代码##

Result:

结果:

##代码##