【发布时间】: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 > 0)
标签: c loops whitespace