在 c 中使用标记化
我正在尝试标记一条线并将其放入二维数组中,到目前为止我已经想出了这个,但我觉得我还很遥远:
/**
* Function to tokenize an input line into seperate tokens
*
* The first arg is the line to be tokenized and the second arg points to
* a 2-dimentional string array. The number of rows of this array should be
* at least MAX_TOKENS_PER_LINE size, and the number of columns (i.e., length
* of each string should be at least MAX_TOKEN_SIZE)
*
* Returns 0 on success and negative number on failure
*/
int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens){
char *tokenPtr;
tokenPtr = strtok(line, " \t");
for(int j =0; j<MAX_TOKEN_SIZE; j++){
while(tokenPtr != NULL){
if(!(tokens[][j] = tokenPtr)){return -1;}
num_tokens++;
tokenPtr = strtok(NULL, " \t");
}
}
return 0;
}
I am trying to tokenize a line and put it into a two dimensional array so far I have come up with this but I feel I am far off:
/**
* Function to tokenize an input line into seperate tokens
*
* The first arg is the line to be tokenized and the second arg points to
* a 2-dimentional string array. The number of rows of this array should be
* at least MAX_TOKENS_PER_LINE size, and the number of columns (i.e., length
* of each string should be at least MAX_TOKEN_SIZE)
*
* Returns 0 on success and negative number on failure
*/
int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens){
char *tokenPtr;
tokenPtr = strtok(line, " \t");
for(int j =0; j<MAX_TOKEN_SIZE; j++){
while(tokenPtr != NULL){
if(!(tokens[][j] = tokenPtr)){return -1;}
num_tokens++;
tokenPtr = strtok(NULL, " \t");
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
希望这应该有效。
Hope this should work.
你应该实现一个有限状态机,我刚刚完成我的 shell 命令 Lexer/Parser (LL)
看:如何手动编写(shell)词法分析器
You should implement a finite state machine, I've just finish my shell command Lexer/Parser (LL)
Look : How to write a (shell) lexer by hand
tokenPtr
未初始化 - 第一次循环时它可能为 NULL,也可能不是 NULL。strtok
有 2 个参数。如果您想拆分多个字符,请将它们全部包含在第二个字符串中。strtok
调用之后,token指针指向你想要的字符串。现在怎么办?你需要一个地方来存放它。也许是一个 char* 数组?或者是一个二维字符数组,如您编辑的原型中。tokens[i]
是 MAX_TOKEN_SIZE 个字符的存储空间。strtok()
返回一个指向字符串(1 个或多个字符的序列)的指针。您需要将一个复制到另一个中。请注意,
char tokens[][MAX]
通常称为二维字符数组。 (或固定长度字符串的一维数组)。二维字符串数组将是 char* tokens[][MAX]tokenPtr
is not initialized - it may or may not be NULL the first time through the loop.strtok
takes 2 arguments. If you want to split on multiple chars, include them all in the 2nd string.strtok
call, token pointer points to the string you want. Now what? You need somewhere to store it.Perhaps an array of char*?Or an 2d array of characters, as in your edited prototype.tokens[i]
is storage for MAX_TOKEN_SIZE characters.strtok()
returns a pointer to a string (a sequence of 1 or more characters ). You need to copy one into the other.Note that
char tokens[][MAX]
is usually referred to as a 2-D array of characters. (or a 1-D array of fixed-length strings). A 2-D array of strings would be char* tokens[][MAX]