gpt4 book ai didi

C 子字符串方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:52:54 25 4
gpt4 key购买 nike

你好,我正在尝试从一个较大的字符串中提取一个小字符串,基本上我得到了一个带有分隔符的字符串,我需要重新排列它。所以可以说我有“@the president#”@显示我必须开始的地方,#是子字符串的结尾。我正在使用 strchr 获取指向 @ 符号的指针,我知道我需要搜索直到找到 # 符号。虽然没有从 x 到 y 的函数,但我不确定如何从 char 指针 a 到符号 #。

    char *garbage = "@the president#";
int count = 0;
char a = strchr(garbage, @);
char *sentence = NULL;
while(start at a, garbage[count] != #){
char sentence[count] = garbage[count];
count++;
}

最佳答案

使用这样的东西:

const char* posAtSign = strchr(searchString,'@');
if (posAtSign != NULL) {
const char* posPoundSign = strchr(posAtSign+1,'#');
if (posPoundSign != NULL) {
const int numChars = posPoundSign - posAtSign - 1;
strncpy(substringBuffer,posAtSign+1,numChars);
}
}

测试代码:

char searchString[] = "@the president#";
char substringBuffer[128];

const char* posAtSign = strchr(searchString,'@');
if (posAtSign != NULL) {
const char* posPoundSign = strchr(posAtSign+1,'#');
if (posPoundSign != NULL) {
const int numChars = posPoundSign - posAtSign - 1;
strncpy(substringBuffer,posAtSign+1,numChars);
substringBuffer[numChars] = '\0';
printf("substring: '%s'", substringBuffer);
}
}

关于C 子字符串方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17554965/

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