简体   繁体   English

用多个空格在C中断开字符串

[英]Breaking a string in C with multiple spaces

Ok, so my code currently splits a single string like this: "hello world" into: 好的,所以我的代码当前将一个字符串分割成这样:“ hello world”成:

hello
world

But when I have multiple spaces in between, before or after within the string, my code doesn't behave. 但是,当我在字符串之间,之前或之后有多个空格时,我的代码不起作用。 It takes that space and counts it as a word/number to be analyzed. 它占用该空间并将其作为要分析的单词/数字。 For example, if I put in two spaces in between hello and world my code would produce: 例如,如果我在hello和world之间放置两个空格,则代码将产生:

hello
(a space character)
world

The space is actually counted as a word/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;
}

I can see the problem area in my loop, but I'm a bit stumped as to how to fix this. 我可以在循环中看到问题区域,但是对于解决此问题我有些困惑。 Any help would be must appreciated. 任何帮助将不胜感激。

From a coding stand point, if you wanted to know how to do this without a library as an exercise, what's happening is your loop breaks after you run into the first delimeter. 从编码的角度来看,如果您想知道如何在没有库的情况下进行练习,那么您遇到的第一个问题就是循环中断。 Then when you loop to the second delimeter, you don't enter the second while loop and print a new line again. 然后,当您循环到第二个定界符时,您无需输入第二个while循环并再次打印新行。 You can put 你可以放

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

Use the standard C library function strtok(). 使用标准的C库函数strtok()。

Rather than redevelop such a standard function. 而不是重新开发这样的标准功能。

Here's the related related manual page . 这是相关的相关手册页

Can use as following in your case: 在您的情况下可以使用以下方法:

#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");
}

As you can observe, each subsequent call to strtok using the same arguments will send you back a char* adressing to the next token. 如您所见,随后每次使用相同参数对strtok的调用都会使您将char *地址返回给下一个标记。

In the case you're working on a threaded program, you might use strtok_r() C function. 如果您正在处理线程程序,则可以使用strtok_r()C函数。

First call to it should be the same as strtok(), but subsequent calls are done passing NULL as the first argument. 对其的第一次调用应与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)
}

Just put the process token logic into a if(counter > 0){...} , which makes malloc happen only when there was a real token. 只需将流程令牌逻辑放入if(counter > 0){...} ,就可以使malloc仅在存在真实令牌时才发生。 like this 像这样

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

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

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