gpt4 book ai didi

c++ - 使用 strtok 分割字符串

转载 作者:搜寻专家 更新时间:2023-10-31 01:53:51 25 4
gpt4 key购买 nike

 char *strings[30];
char Policystr[4096] = "the|string|to|split";
char delims[] = "|";
int i = 0;

strings[i] = strtok( Policystr, delims )
while( strings[i] != NULL )
{
MessageBoxA(NULL,strings[i],"stringsComparison",MB_OK);
strings[++i] = strtok( NULL, delims );
}
for ( int j = 0; j < i; j++ )
{
MessageBoxA(NULL,strings[j],"strings",MB_OK);
}

我是 C++ 的新手,如果我能够在第二个循环中打印相同的字符串,我会在第一个循环中获取所有字符串,我不知道,我没有得到

提前致谢

最佳答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
char *strings[30];
char Policystr[4096] = "the|string|to|split";
char delims[] = "|";

int i = 0;
strings[i] = strtok( Policystr, delims );
while( strings[i] != NULL )
{
printf("%d '%s'\n", i, strings[i]);
strings[++i] = strtok( NULL, delims );
}

for ( int j = 0; j < i; j++ )
{
printf("%d '%s'\n", j, strings[j]);
}
}

输出:

0 'the'
1 'string'
2 'to'
3 'split'
0 'the'
1 'string'
2 'to'
3 'split'

关于c++ - 使用 strtok 分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10513841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com