可以在这里找到类似的 C 语言。
我需要根据字符示例的出现将句子分解为字符数组:%
示例
如果我的句子是my%healthy%dog,那么我应该能够分别得到my、healthy和dog。 这也可能处于循环中。
发送
最佳答案
strtok
是实现字符串标记化的标准 C 函数。
#include <stdio.h>
#include <string.h>
int main()
{
char c[] = "my%healthy%dog";
char *token = strtok(c, "%");
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, "%");
}
return 0;
}
$ ./a.exe
my
healthy
dog
另请注意,strtok
在内部使用静态变量,因此它不是线程安全的。为了线程安全,您必须使用 strtok_r
功能。
关于c - 基于字符将字符串分解为数组的有效方法,例如 C 中的%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1505128/