輸入(字符串):foo $$ foo ## foo []
搜索(字符串):foo
輸出(數組):$$ ,## ,[]
我嘗試了:
char * str = "foo $$ foo ## foo []";
char * s = "foo";
int buf_len = 0;
int len = strlen(s);
int i = 0;
char ** buffer = malloc(MAX_BUFFER_SIZE);
char * tmpbuf = malloc(MAX_BUFFER_SIZE);
char * p = str;
char ** buf = buffer;
char * tbuf = tmpbuf;
while(*p)
{
if(*p == *s)
{
while(*p == *(s + i))
{
i++;
p++;
}
if(i == len)
{
*buf ++ = tbuf;
memset(tbuf,0,buf_len);
i = buf_len = 0;
}
}
else
{
*tbuf ++= *p;
buf_len ++;
}
p++;
}
*buf ++= NULL;
int x;
for(x = 0; buffer[x]; x++)
{
printf("%s\n", buffer[x]);
}
free(buffer);
free(tmpbuf);
顯示以下輸出:
$$ ## []
## []
[]
,但預期是:
$$
##
[]
如何解決這一問題?
why -1?可以解釋一下嗎? – Jack 2012-04-03 20:20:55
可能的重複[在C#中拆分字符串](http://stackoverflow.com/questions/4545621/split-string-at-in-c) – 2012-04-03 22:19:37
@BoPersson:不。這不是'char'問題。我想分割'string'。 – Jack 2012-04-04 00:39:40