gpt4 book ai didi

c - 在c中标记原始字符串

转载 作者:行者123 更新时间:2023-11-30 15:52:26 25 4
gpt4 key购买 nike

我想以一种非常特殊的方式标记一个字符串。

我有以下字符串,由 3 组单词组成,并用空格分隔:

string = abc def ghi

问题是我想将 string 变量的所有内容加载到另一个字符串中,直到第二个空格。也就是说,我想得到:

result = abc def

不仅是 abc(该解决方案在其他论坛中)。请注意,每个单词的长度可能不同。

我该怎么做?

最佳答案

I would like to load in one string all the content of string variable until the second space

怎么样:

char *space = strchr(string, ' ');
if (!space)
error;
space++;
space = strchr(space, ' ');
if (!space)
error;

或者,如果您知道总会有 3 个单词,请执行单个 strrchr(相反)。或者可能执行 2 个 sscanfs,然后连接字符串,或者执行 2 个 strtok 等。

关于c - 在c中标记原始字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14444353/

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