繁体   English   中英

使用C从字符串计数并获取整数

[英]Count and get integers from a string using C

我在自学C编程。 我试图计算给定字符串中存在的int数量,这些字符串之间用空格隔开。

exp:输入str =“ 1 2 11 84384 0 212”输出应为:1、2、11、84384、0、212总int = 6

当我尝试。 因为我在这里没有使用正确的方法,所以它为我提供了所有有意义的数字输出。

我知道在python中我可以使用str.split(“”)函数来快速完成我的工作。

但是我想在C中尝试类似的方法。尝试创建自己的split方法。

#include <stdio.h>
#include <string.h>
void count_get_ints(const char *data) {
    int buf[10000];
    int cnt = 0, j=0;
    for (int i=0; i<strlen(data); i++) {
        if (isspace(data[i] == false)
            buf[j] = data[i]-'0';
            j++;
    }
    printf("%d", j);

}

// when I check the buffer it includes all the digits of the numbers.
// i.e for my example.
// buf = {1,2,1,1,8,4,3,8,4,0,2,1,2} 
// I want buf to be following 
// buf = {1,2,11,84384,0,212}

我知道这不是解决此问题的正确方法。 一种跟踪上一个并使用遇到的非空格数字动态创建内存的方法。 但是我不确定这种方法是否有帮助。

您要逐步构建数字,直到遇到空格,然后将其放入数组中。 您可以通过乘以10,然后每次添加下一位数字来完成此操作。

void count_get_ints(const char *data) {
    int buf[10000];
    int j = 0;
    int current_number = 0;

    // Move this outside the loop to eliminate recalculating the length each time
    int total_length = strlen(data); 
    for (int i=0; i <= total_length; i++) {
        // Go up to 1 character past the length so you 
        //   capture the last number as well
        if (i == total_length || isspace(data[i])) {
            // Save the number, and reset it
            buf[j++] = current_number;
            current_number = 0;
        }
        else {
            current_number *= 10;
            current_number += data[i] - '0';
        }
    }
}

我认为strtok将提供更清洁的解决方案,除非您真的想遍历字符串中的每个字符。 自从我做C以来已经有一段时间了,所以请原谅下面代码中的任何错误,希望它能给您正确的想法。

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

int main() {
    char str[19] = "1 2 11 84384 0 212";
    const char s[2] = " ";
    char *token;
    int total;

    total = 0;
    token = strtok(str, s);

    while (token != NULL) {
        printf("%s\n", token);
        total += atoi(token);
        token = strtok(NULL, s);
    }
    printf("%d\n", total);

    return 0;
}

您可以通过执行c-'0'来检查每个字符的ascii值。 如果介于[0,9]之间,则为整数。 通过使用状态变量,当您通过检查给定字符是否为空格而位于整数内时,可以通过忽略空格来跟踪计数。 另外,您不需要缓冲区, 如果数据大于10,000,并且将缓冲区的末尾写入pass会发生什么情况,则会发生未定义的行为。 此解决方案不需要缓冲区。

编辑,解决方案现在将打印字符串中的整数

void count_get_ints(const char *data) {
   int count = 0;
   int state = 0;
   int start = 0;
   int end = 0;
   for(int i = 0; i<strlen(data); i++){
      int ascii = data[i]-'0';
      if(ascii >= 0 && ascii <= 9){
         if(state == 0){
            start = i;
         }
         state = 1;
      }else{
         //Detected a whitespace
         if(state == 1){
            count++;
            state = 0;
            end = i;
            //Print the integer from the start to end spot in data
            for(int j = start; j<end; j++){
                printf("%c",data[j]);
            }
            printf(" ");
         }
      }
   }
   //Check end
   if(state == 1){
      count++;
      for(int j = start; j<strlen(data); j++){
          printf("%c",data[j]);
      }
      printf(" ");
   }
   printf("Number of integers %d\n",count);
}

我相信,执行此操作的标准方法是使用sscanf%n格式说明符来跟踪读取了多少字符串。

您可以从大型数组开始阅读-

int array[100];

然后,您可以继续从字符串中读取整数,直到无法读取为止或读完100。

int total = 0;
int cont = 0;
int ret = 1;
while(ret == 1 && total < 100) {
    ret = sscanf(input, "%d%n", &array[total++], &cont);
    input += cont;
}
total--;
printf("Total read = %d\n", total);

array包含所有读取的数字。

这是演示

使用strtol示例

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>

int count_get_ints(int output[], int output_size, const char *input) {
    const char *p = input;
    int cnt;

    for(cnt = 0; cnt < output_size && *p; ++cnt){
        char *endp;
        long n;
        errno = 0;
        n = strtol(p, &endp, 10);
        if(errno == 0 && (isspace((unsigned char)*endp) || !*endp) && INT_MIN <= n && n <= INT_MAX){
            output[cnt] = n;
            while(isspace((unsigned char)*endp))
                ++endp;//skip spaces
            p = endp;//next parse point
        } else {
            fprintf(stderr, "invalid input '%s' in %s\n", p, __func__);
            break;
        }
    }
    return cnt;
}

int main(void) {
    const char *input = "1 2 11 84384 0 212";
    int data[10000];
    int n = sizeof(data)/sizeof(*data);//number of elements of data

    n = count_get_ints(data, n, input);
    for(int i = 0; i < n; ++i){
        if(i)
            printf(", ");
        printf("%d", data[i]);
    }
    puts("");
}

假设您的字符串中没有任何非数字,则只需计算空格数+ 1即可找到字符串中的整数数,如以下伪代码所示:

for(i = 0; i < length of string; i++) {
   if (string x[i] == " ") {
      Add y to the list of strings
      string y = "";
      counter++;
   }

   string y += string x[i]
}

numberOfIntegers =  counter + 1;

同样,这将读取空白之间的数据。 请记住,这是伪代码,因此语法不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM