标记字符串并将其 return 作为数组
Tokenizing a string and return it as an array
我正在尝试标记传递的字符串,将标记存储在一个数组中,然后 return 它。我在 ubuntu 上 运行。显然,当谈到这种语言时,我感到很困惑。
示例输入:coinflip 3
我的代码思路如下:
take: string
if string = null: return null
else:
while temp != null
token[i++] = temp
temp = get next token
return
这是我目前的解决方案。分隔符是空格。 C 已经有一段时间不是我的强项了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Clears the screen and prompts the user
void msg()
{
static int init = 1;
if(init)
{
printf("\e[1;1H\e[2J");
init = 0;
}
printf("%s", "uab_sh > ");
}
//Reads in line
char *readIn(void)
{
char param[101];
fgets(param, 101, stdin);
return param;
}
//parse string - still working out the kinks :)
char **parseString(char *cmd)
{
char delim[] = " ";
char* temp = strtok(cmd, delim);
if (temp == " ")
{
return NULL;
}
else
{
int i = 0;
char** tokens = malloc(3 * sizeof(char*));
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}
//Command
int command(char ** cmd)
{
int pid;
if (cmd[0] != NULL)
{
pid = fork();
if (pid == 0)
{
exit(0);
}
else if (pid < 0)
{
perror("Something went wrong...");
}
}
else
return 1;
}
int main()
{
char *line;
char **cmd;
int stat = 0;
while (1)
{
msg();
line = readLine();
cmd = parseString(line);
stat = command(cmd);
if (stat == 1)
{
break;
}
}
return 0;
}
当前错误:
main.c: In function ‘readIn’:
main.c:24:9: warning: function returns address of local variable [-Wreturn-local-addr]
return param;
^~~~~
main.c: In function ‘parseString’:
main.c:32:11: warning: comparison with string literal results in unspecified behavior [-Waddress]
if (temp == " ")
^~
main.c: In function ‘command’:
main.c:59:9: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
pid = fork();
^~~~
main.c: In function ‘main’:
main.c:82:10: warning: implicit declaration of function ‘readLine’; did you mean ‘readIn’? [-Wimplicit-function-declaration]
line = readLine();
^~~~~~~~
readIn
main.c:82:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
line = readLine();
^
main.c: In function ‘command’:
main.c:71:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
好吧,除了这段代码不能处理不超过 3 个标记的事实,它还有另一个基本问题:它会return 一个非法的内存指针。 temp
和 tokens
是位于 parseString()
函数堆栈帧内的变量。所以当它执行完成时,那些变量就会消失。这里理想的解决方案是在堆中分配tokens
。
这是我的解决方案:
char** parseString(char* cmd)
{
char delimiters[] = " ";
char* temp = strtok(cmd, delimiters);
//If temp is NULL then the string contains no tokens
if (temp == NULL)
{
return NULL;
}
else
{
int i = 0;
char** tokens = malloc(3*sizeof(char*));
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}
编译器已经报告了这个函数
//Read-in string
char *readIn(void)
{
char param[101];
fgets(param, 101, stdin);
return param;
}
具有未定义的行为,因为它 return 指向本地数组 param
的指针,该数组在退出函数后将不再存在。
在这个函数中
char *parseString(char* cmd)
{
char* temp = strtok(cmd, " ");
if (cmd == NULL)
{
return temp;
}
else
{
int i = 0;
char *tokens[3];
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}
存在同样的问题(如果不考虑错误的实现),而且 returned 表达式的类型
return tokens;
不对应函数的return类型,因为return语句中的表达式是char **
类型,而函数的return类型是char *
.
我相信对你来说最困难的是编写将字符串拆分为标记的函数。
它可以像下面的演示程序所示那样。该函数为指向令牌的指针数组动态分配内存。如果分配失败,函数 returns NULL。否则,函数 returns 指向动态分配的指针数组的第一个元素的指针。数组的最后一个元素包含 NULL。该元素可用于确定指向数组中标记的实际指针的数量。
给你。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char ** parseString( char *cmd )
{
char **tokens = malloc( sizeof( char * ) );
*tokens = NULL;
size_t n = 1;
const char *delim = " \t";
char *p = strtok( cmd, delim );
int success = p != NULL;
while ( success )
{
char **tmp = realloc( tokens, ( n + 1 ) * sizeof( char * ) );
if ( tmp == NULL )
{
free( tokens );
tokens = NULL;
success = 0;
}
else
{
tokens = tmp;
tokens[n - 1] = p;
tokens[n] = NULL;
++n;
p = strtok( NULL, delim );
success = p != NULL;
}
}
return tokens;
}
int main(void)
{
char cmd[] = "Many various and unique commands";
char **tokens = parseString( cmd );
if ( tokens != NULL )
{
for ( char **p = tokens; *p != NULL; ++p )
{
puts( *p );
}
}
free( tokens );
return 0;
}
程序输出为
Many
various
and
unique
commands
这是一个来来回回的递归解决方案。这个习语使用调用堆栈作为临时 space 以便只分配令牌数组一次。由于它需要很少的簿记,因此代码被彻底简化了。不是每个人都会觉得这很有吸引力。
#include <stdlib.h>
#include <string.h>
static char** helper(char* token, int argc) {
char** retval = token ? parseString(strtok(NULL, " "), argc + 1)
: malloc((argc + 1) * sizeof *retval);
if (retval) retval[argc] = token;
return retval;
}
char** parseString(char* cmd) {
return helper(strtok(cmd, " "), 0);
}
当然,递归实现意味着包含大量标记的行可能会溢出堆栈。实际上,我并不担心这一点,因为 helper
的堆栈帧非常小,没有 VLA、alloca
甚至未初始化的局部变量。因此堆栈溢出将 (1) 需要大量令牌,并且 (2) 可靠地命中保护页,如果有,则终止进程。如果您使用的 OS 没有堆栈保护,您可以检查函数中的递归深度,因为第二个参数跟踪深度。
我正在尝试标记传递的字符串,将标记存储在一个数组中,然后 return 它。我在 ubuntu 上 运行。显然,当谈到这种语言时,我感到很困惑。
示例输入:coinflip 3
我的代码思路如下:
take: string
if string = null: return null
else:
while temp != null
token[i++] = temp
temp = get next token
return
这是我目前的解决方案。分隔符是空格。 C 已经有一段时间不是我的强项了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Clears the screen and prompts the user
void msg()
{
static int init = 1;
if(init)
{
printf("\e[1;1H\e[2J");
init = 0;
}
printf("%s", "uab_sh > ");
}
//Reads in line
char *readIn(void)
{
char param[101];
fgets(param, 101, stdin);
return param;
}
//parse string - still working out the kinks :)
char **parseString(char *cmd)
{
char delim[] = " ";
char* temp = strtok(cmd, delim);
if (temp == " ")
{
return NULL;
}
else
{
int i = 0;
char** tokens = malloc(3 * sizeof(char*));
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}
//Command
int command(char ** cmd)
{
int pid;
if (cmd[0] != NULL)
{
pid = fork();
if (pid == 0)
{
exit(0);
}
else if (pid < 0)
{
perror("Something went wrong...");
}
}
else
return 1;
}
int main()
{
char *line;
char **cmd;
int stat = 0;
while (1)
{
msg();
line = readLine();
cmd = parseString(line);
stat = command(cmd);
if (stat == 1)
{
break;
}
}
return 0;
}
当前错误:
main.c: In function ‘readIn’:
main.c:24:9: warning: function returns address of local variable [-Wreturn-local-addr]
return param;
^~~~~
main.c: In function ‘parseString’:
main.c:32:11: warning: comparison with string literal results in unspecified behavior [-Waddress]
if (temp == " ")
^~
main.c: In function ‘command’:
main.c:59:9: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
pid = fork();
^~~~
main.c: In function ‘main’:
main.c:82:10: warning: implicit declaration of function ‘readLine’; did you mean ‘readIn’? [-Wimplicit-function-declaration]
line = readLine();
^~~~~~~~
readIn
main.c:82:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
line = readLine();
^
main.c: In function ‘command’:
main.c:71:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
好吧,除了这段代码不能处理不超过 3 个标记的事实,它还有另一个基本问题:它会return 一个非法的内存指针。 temp
和 tokens
是位于 parseString()
函数堆栈帧内的变量。所以当它执行完成时,那些变量就会消失。这里理想的解决方案是在堆中分配tokens
。
这是我的解决方案:
char** parseString(char* cmd)
{
char delimiters[] = " ";
char* temp = strtok(cmd, delimiters);
//If temp is NULL then the string contains no tokens
if (temp == NULL)
{
return NULL;
}
else
{
int i = 0;
char** tokens = malloc(3*sizeof(char*));
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}
编译器已经报告了这个函数
//Read-in string
char *readIn(void)
{
char param[101];
fgets(param, 101, stdin);
return param;
}
具有未定义的行为,因为它 return 指向本地数组 param
的指针,该数组在退出函数后将不再存在。
在这个函数中
char *parseString(char* cmd)
{
char* temp = strtok(cmd, " ");
if (cmd == NULL)
{
return temp;
}
else
{
int i = 0;
char *tokens[3];
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}
存在同样的问题(如果不考虑错误的实现),而且 returned 表达式的类型
return tokens;
不对应函数的return类型,因为return语句中的表达式是char **
类型,而函数的return类型是char *
.
我相信对你来说最困难的是编写将字符串拆分为标记的函数。
它可以像下面的演示程序所示那样。该函数为指向令牌的指针数组动态分配内存。如果分配失败,函数 returns NULL。否则,函数 returns 指向动态分配的指针数组的第一个元素的指针。数组的最后一个元素包含 NULL。该元素可用于确定指向数组中标记的实际指针的数量。
给你。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char ** parseString( char *cmd )
{
char **tokens = malloc( sizeof( char * ) );
*tokens = NULL;
size_t n = 1;
const char *delim = " \t";
char *p = strtok( cmd, delim );
int success = p != NULL;
while ( success )
{
char **tmp = realloc( tokens, ( n + 1 ) * sizeof( char * ) );
if ( tmp == NULL )
{
free( tokens );
tokens = NULL;
success = 0;
}
else
{
tokens = tmp;
tokens[n - 1] = p;
tokens[n] = NULL;
++n;
p = strtok( NULL, delim );
success = p != NULL;
}
}
return tokens;
}
int main(void)
{
char cmd[] = "Many various and unique commands";
char **tokens = parseString( cmd );
if ( tokens != NULL )
{
for ( char **p = tokens; *p != NULL; ++p )
{
puts( *p );
}
}
free( tokens );
return 0;
}
程序输出为
Many
various
and
unique
commands
这是一个来来回回的递归解决方案。这个习语使用调用堆栈作为临时 space 以便只分配令牌数组一次。由于它需要很少的簿记,因此代码被彻底简化了。不是每个人都会觉得这很有吸引力。
#include <stdlib.h>
#include <string.h>
static char** helper(char* token, int argc) {
char** retval = token ? parseString(strtok(NULL, " "), argc + 1)
: malloc((argc + 1) * sizeof *retval);
if (retval) retval[argc] = token;
return retval;
}
char** parseString(char* cmd) {
return helper(strtok(cmd, " "), 0);
}
当然,递归实现意味着包含大量标记的行可能会溢出堆栈。实际上,我并不担心这一点,因为 helper
的堆栈帧非常小,没有 VLA、alloca
甚至未初始化的局部变量。因此堆栈溢出将 (1) 需要大量令牌,并且 (2) 可靠地命中保护页,如果有,则终止进程。如果您使用的 OS 没有堆栈保护,您可以检查函数中的递归深度,因为第二个参数跟踪深度。