gpt4 book ai didi

c - 如何删除C中的特殊字符?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:02:08 24 4
gpt4 key购买 nike

有一个字符串

char *message = "hello#world#####.......";

如何删除所有“#”并返回“helloworld”?

在 Ruby 中我可以使用 gsub 来处理它

最佳答案

在 C 中,你必须自己做。例如:

#include <string.h>

char *remove_all(const char *source, char c)
{
char *result = (char *) malloc(strlen(source) + 1);
char *r = result;
while (*source != '\0')
{
if (*source != c)
*r++ = *source;
source++;
}

*r = '\0';
return result;
}

请注意,在该实现中,调用者必须释放结果字符串。

关于c - 如何删除C中的特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4546324/

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