【问题标题】:How do I store an array in three different ones based on delimiters如何根据分隔符将数组存储在三个不同的数组中
【发布时间】:2019-04-15 10:16:30
【问题描述】:

我想根据/ 作为分隔符将输入数组分成三个不同的数组。

我尝试了(可能是幼稚的)方法,通过使用getcharwhile 将字符读入数组并使用计数器计算@987654327 的次数,将输入字符串存储到不同的数组中@ 出现。

根据这个数字我会使用:

if (slashcounter == 0) {
      destinationarray[i++] = c;
}

将其存储到正确的数组中。下面是完整的实现。

请注意,我尝试仅使用 stdio.h 来执行此操作

#include <stdio.h>

char c; 
char replace[80], toBeReplaced[80], input[80], testInput[80];
int i = 0;
int slashcounter = 0;

int main(){

    puts("Enter a line of text: ");

    while (( c = getchar()) != '\n'){

        if (c == '/') {
            slashcounter++;
        }

        if (slashcounter == 0) {
            replace[i++] = c;
        }

        else if (slashcounter == 1) {
            toBeReplaced[i++] = c;
        }

        else if (slashcounter == 2) {
            input[i++] = c;
        }


    }
    //debug purpose
    puts("The arrays have the following content\n");
    puts("replace[]:\n");
    puts(replace);
    puts("\n");
    puts("toBeReplaced[]:\n");
    puts(toBeReplaced);
    puts("\n");
    puts("input[]:\n");
    puts(input);
    printf("Slashcounter = %d\n",slashcounter);



    return 0;

不幸的是,发生的事情是:第一个单词,即第一个斜杠之前的单词被正确存储,但其他两个是空的。

我做错了什么

当前输出与输入this/test/fails

Enter a line of text: 
this/test/fails
The arrays have the following content

replace[]:

this


toBeReplaced[]:




input[]:


Slashcounter = 2
Program ended with exit code: 0

附言我还想确保/s 不在输出数组中。

感谢您的帮助。

【问题讨论】:

  • 您可能对strtok 函数感兴趣。
  • 至于你目前的程序,我推荐你learn how to debug your programs。密切注意变量i 及其值。并且不要忘记char 字符串实际上称为 null-terminated 字节字符串。
  • 我用 strtok 管理它,但这可以在 string.h 中找到,我只能使用 stdio.h 和函数 getchar()、puts() 和 putchar()
  • strtok 应该是用户,但它来自string.h,请手动处理,您将需要to read input string by charfound each position of delimiter,然后是split to tokens(从0 索引到第一次出现,从“firstOccur+1”索引到第二个等等。
  • char c; 必须是 intgetchar() 不直观地返回 int,而不是 char

标签: c


【解决方案1】:

您的代码中有两个直接问题,首先您错过了添加一个空字符来结束每个子字符串,其次您在读取 ​​/ 时从未将索引重置为 0

其他问题是您不检查是否会写出数组,并且您不管理 EOF

你也一直在测试 slashcounter 的值,这是相当昂贵的,你可以有 3 个循环或使用指向数组的指针来填充等

也没有理由使用全局变量,在main

中都可以是局部的

改动最小的示例:

#include <stdio.h>

int main(){
  int c;
  char replace[80], toBeReplaced[80], input[80];
  int i = 0;
  int slashcounter = 0;

  puts("Enter a line of text: ");

  while (( c = getchar()) != '\n') {
    if (c == EOF) {
      fprintf(stderr, "unexpected EOF");
      return -1;
    }

    if (c == '/') {
      if (slashcounter == 0) {
        replace[i] = 0;
      }
      else if (slashcounter == 1) {
        toBeReplaced[i] = 0;
      }
      else if (slashcounter == 2) {
        input[i] = c;
      }
      i = 0;
      slashcounter++;
    }
    else if (slashcounter == 0) {
      if (i != (sizeof(replace) - 2))
        replace[i++] = c;
    }
    else if (slashcounter == 1) {
      if (i != (sizeof(toBeReplaced) - 2))
        toBeReplaced[i++] = c;
    }
    else if (slashcounter == 2) {
      if (i != (sizeof(input) - 2))
        input[i++] = c;
    }
  }

  if (slashcounter == 0) {
    replace[i] = 0;
    toBeReplaced[0] = 0;
    input[0] = 0;
  }
  else if (slashcounter == 1) {
    toBeReplaced[i] = 0;
    input[0] = 0;
  }
  else if (slashcounter == 2) {
    input[i] = 0;
  }

  //debug purpose
  puts("The arrays have the following content\n");
  puts("replace[]:\n");
  puts(replace);
  puts("\n");
  puts("toBeReplaced[]:\n");
  puts(toBeReplaced);
  puts("\n");
  puts("input[]:\n");
  puts(input);
  printf("Slashcounter = %d\n",slashcounter);

  return 0;
}

注意我使用 int 表示 c 来处理 EOF 并删除了无用的数组 testInput

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra s.c
pi@raspberrypi:/tmp $ ./a.out
Enter a line of text: 
this/test/fails
The arrays have the following content

replace[]:

this


toBeReplaced[]:

test


input[]:

fails
Slashcounter = 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-19
    • 2017-05-06
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多