【问题标题】:Breaking a string in C with multiple spaces在 C 中用多个空格分隔字符串
【发布时间】:2015-06-29 10:01:56
【问题描述】:

好的,所以我的代码目前将这样的单个字符串:“hello world”拆分为:

hello
world

但是当我在字符串之间、之前或之后有多个空格时,我的代码将无法正常运行。它占用该空间并将其计为要分析的单词/数字。例如,如果我在 hello 和 world 之间放置两个空格,我的代码会生成:

hello
(a space character)
world

空格实际上算作一个单词/token。

int counter = 0;
int index = strcur->current_index;
char *string = strcur->myString;

char token_buffer = string[index];

while(strcur->current_index <= strcur->end_index)
{
    counter = 0;
    token_buffer = string[counter+index];
    while(!is_delimiter(token_buffer) && (index+counter)<=strcur->end_index)//delimiters are: '\0','\n','\r',' '
    {
        counter++;
        token_buffer = string[index+counter];
    }

    char *output_token = malloc(counter+1);
    strncpy(output_token,string+index,counter);
    printf("%s \n", output_token);
    TKProcessing(output_token);

    //update information
    counter++;    
    strcur->current_index += counter;
    index += counter;
}

我可以在我的循环中看到问题区域,但我对如何解决这个问题有点困惑。任何帮助将不胜感激。

【问题讨论】:

  • 阅读一个单词后,不要使用单个counter++,而是在while循环中再次使用is_delimiter函数。
  • 在进行 output_token 处理之前添加一个控件if(counter &gt; 0)

标签: c loops whitespace


【解决方案1】:

从编码的角度来看,如果您想知道如何在没有库的情况下执行此操作作为练习,那么发生的情况是在您遇到第一个分隔符后循环中断。然后,当您循环到第二个分隔符时,您不会进入第二个 while 循环并再次打印新行。你可以放

//update information
while(is_delimiter(token_buffer) && (index+counter)<=strcur->end_index)
{
    counter++;
    token_buffer = string[index+counter];
}

【讨论】:

    【解决方案2】:

    使用标准 C 库函数 strtok()。

    而不是重新开发这样的标准功能。

    这是相关的related manual page

    在你的情况下可以使用如下:

    #include <string.h>
    char *token;    
    
    token = strtok (string, " \r\n");
    // do something with your first token
    while (token != NULL)
    {
      // do something with subsequents tokens
      token = strtok (NULL, " \r\n");
    }
    

    如您所见,使用相同参数对 strtok 的每次后续调用都会向您发送回一个 char* 寻址到下一个令牌。

    如果您正在处理线程程序,您可以使用 strtok_r() C 函数。

    第一次调用它应该与 strtok() 相同,但随后的调用是通过传递 NULL 作为第一个参数来完成的。 :

    #include <string.h>
    char *token;
    char *saveptr;
    
    token = strtok_r(string, " \r\n", &saveptr)
    // do something with your first token
    while (token != NULL)
    {
       // do something with subsequents tokens
       token = strtok_r(NULL, " \r\n", &saveptr)
    }
    

    【讨论】:

    • do something with your tokenized word 评论为//do something with your tokenized word。虽然这并不重要。
    • 另一个有用的strtok参考
    • strtok_r 不标准
    • 这里使用 strtok_r 代替 strtok 没有任何好处。
    • @nouney true 这对于符合 posix 的系统来说是一个很好的解决方案。对于 windows 可以为 strtok_s 添加别名
    【解决方案3】:

    只需将进程令牌逻辑放入if(counter &gt; 0){...},这使得malloc只有在有真正令牌时才会发生。像这样

    if(counter > 0){ // it means has a real word, not delimeters 
       char *output_token = malloc(counter+1);
       strncpy(output_token,string+index,counter);
       printf("%s \n", output_token);
       TKProcessing(output_token);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2018-04-20
      • 2016-11-17
      • 1970-01-01
      相关资源
      最近更新 更多