【问题标题】:How to get numbers from a character array in C [duplicate]如何从C中的字符数组中获取数字[重复]
【发布时间】:2020-03-25 08:05:21
【问题描述】:

我正在为我的班级做一个项目,但遇到了障碍。假设我有一个空格分隔的字符数组(字符串),其中包含数字(每个数字后跟一个空格字符)。假设它看起来像这样:

0 1 15 10 6 2

其中的项目数量永远不会恒定,因此我无法使用sscaf() 获取所有数字。我尝试将它作为字符循环,但我最终将两位数分开,搞砸了。

有人可以指导我如何获取字符串中的每个数字并将其保存到 int 数组而不分离两位数吗?我正在用 C 写这个

提前致谢

【问题讨论】:

  • 你需要两个指针,一个头和一个尾。移动尾部直到你碰到一个空白空间,然后从头部减去尾部并分配一个新的字符 * 然后将所有字符从头到尾添加到新的字符 * - 这样做直到尾部位于字符串的末尾
  • 您可以在sscanf()s 返回值的帮助下遍历未知数量的数字。只是不要遵循常见的弊端来忽略它。

标签: c arrays string loops character


【解决方案1】:

另一个解决问题的方法是浏览字符串并将字符添加到缓冲区中,直到找到一个空白区域,然后使用一个函数获取该缓冲区并将其转换为数字。

但在此之前我们必须计算空格的数量,因为这将告诉我们我们拥有的整数数量,或者当数字多于默认值时,您可以为整数数组和 realloc 设置一个默认值价值。

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

#define WHITE_SPACE (0x20)


int32_t
get_len_numbers(const char *str) {

    int32_t     n;
    const char *ptr;

    ptr = str;
    n   = 0;

    for(; *ptr != 0; ++ptr)
        n += (*ptr == ' ');

    return(n + 1);

}

int32_t
stoi(const char *number) {

    int32_t n;

    n = 0;

    for(; *number != 0; ++number)
        n = (n * 10) + (*number & 0x0F);

    return(n);

}


int main(void) {

    int32_t           len;
    register int32_t  i;
    register int32_t  j;

    uint8_t           buf[32];
    char              *str;

    int32_t           *numbers;

    str     = "1 5 25 30 99";

    len     = get_len_numbers(str);
    numbers = malloc(sizeof(int) * len);

    if(numbers == NULL)
        exit(EXIT_FAILURE);

    for(i = 0, j = 0; ; ++str) {

        if(*str == WHITE_SPACE || *str == 0) {

            buf[i] = 0;

            numbers[j++] = stoi(&buf[0]);

            i = 0;

            if(*str == 0)
                break;

        } else {

            buf[i++] = *str;

        }


    }

    return(0);
}

【讨论】:

    【解决方案2】:

    如何获取字符串中的每个数字并将其保存到 int 数组而不分离两位数?

    1. 扫描字符串一次,找到int的号码
    2. 定义数组
    3. 再次扫描并保存到数组中

    ...

    const char *s = "0 1 15 10 6 2 ";
    int count = scans_ints(s, NULL);
    if (count <= 0) Handle_Bad_intput();
    else {
      int arr[count];
      sscan_ints(s, arr);
    }
    

    可能sscan_ints()(未经测试)

    #include <ctype.h>
    #include <limits.h>
    
    int sscan_ints(const char *s, int *arr) {
      int count = 0;
      for (;;) {
        // Consume leading spaces: ease detection of end-of-line
        while (isspace((unsigned char) *s)) s++;
        if (*s == '\0') break;
        char *endptr;  // Location where conversion ended
        errno = 0;
        long num = strtol(s, &endptr, 10);
        if (s == endptr) return -1;  // non-numeric text
        if (errno || num < INT_MIN || num > INT_MAX) return -1; // number too big
        if (arr) {
          arr[count] = (int) num;
        }
        count++;
        s = endptr;
      }
      return count;
    }  
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2020-05-19
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      相关资源
      最近更新 更多