解析在C
不同的空格字符数的字符串我是很新,C和尝试写将解析一个字符串,如函数:解析在C
“这(5位在这里)是(1 space here) a (此处为2个空格) string。“
的函数头将具有指针传递的字符串,如:
bool Class::Parse(unsigned char* string)
最后,我想无论解析的单词之间的空格数的每个字,并存储在一个动态数组中的单词。
原谅愚蠢的问题... 但是如果我遍历每个字符,最有效的方法是什么?那是如何存储字符串的?所以,如果我是开始迭代:
while ((*string) != '\0') {
--print *string here--
}
会是这样打印出
T
h
i... etc?
非常感谢您的帮助,您可以提供。
从http://www.cplusplus.com/reference/clibrary/cstring/strtok/
/* strtok example */
#include <stdio.h>
#include <string.h>
int main()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-"); /* split the string on these delimiters into "tokens" */
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-"); /* split the string on these delimiters into "tokens" */
}
return 0;
}
分割字符串 “ - 这,样本串。”为标记:
This
a
sample
string
感谢0verbose的格式。斯图尔特 – 2011-06-02 19:20:04
不客气。也许试着向他解释一下分隔符。您还添加了“,.-”,这对答案没有用处。 – Heisenbug 2011-06-02 19:23:05
首先,C没有类,所以在C程序中,你可能会用一个原型定义功能更像是下列之一:
char ** my_prog_parse(char * string) {
/* (returns a malloc'd array of pointers into the original string, which has had
* \0 added throughout) */
char ** my_prog_parse(const char * string) {
/* (returns a malloc'd NULL-terminated array of pointers to malloc'd strings) */
void my_prog_parse(const char * string, char buf, size_t bufsiz,
char ** strings, size_t nstrings)
/* builds a NULL-terminated array of pointers into buf, all memory
provided by caller) */
然而,这是完全可能使用C++ C风格的字符串...
你可以写你的循环为
while (*string) { ... ; string++; }
,它将编译成与现代优化编译器完全相同的汇编器。是的,这是迭代C风格字符串的正确方法。
看看功能strtok
,strchr
,strstr
和strspn
......其中一个可能会帮助您构建解决方案。
我不会在C中做任何非平凡的解析,这太费力了,这种语言不适合。但是如果你的意思是C++,并且看起来像你一样,因为你编写了Class :: Parse,那么编写递归下降解析器非常容易,而且你不需要重新发明*。例如,如果编译器支持C++ 0x,则可以采用Spirit或AX。例如,AX中的解析器可以写成几行:
// assuming you have 0-terminated string
bool Class::Parse(const char* str)
{
auto space = r_lit(' ');
auto string_rule = "This" & r_many(space, 5) & space & 'a' & r_many(space, 2)
& "string" & r_end();
return string_rule(str, str + strlen(str)).matched;
}
您是在谈论C还是C++? – Heisenbug 2011-06-02 19:18:53