admin 管理员组文章数量: 1086019
2024年4月17日发(作者:transaction是什么意思啊)
c语言replace函数用法
C语言标准库中没有replace函数,需要使用字符串操作函数
来实现字符串的替换。下面是使用字符数组和字符串操作函数
实现替换的示例代码:
```c
#include
#include
int main() {
char str[100] = "hello world, world";
char old_str[20] = "world";
char new_str[20] = "c language";
int old_len = strlen(old_str);
int new_len = strlen(new_str);
char *pos = strstr(str, old_str);
while (pos != NULL) {
memmove(pos + new_len, pos + old_len, strlen(pos + old_len)
+ 1);
memcpy(pos, new_str, new_len);
pos = strstr(pos + new_len, old_str);
}
printf("%sn", str);
return 0;
}
```
以上代码将在字符串"hello world, world"中将"world"替换成"c
language",输出结果为:"hello c language, c language"。
版权声明:本文标题:c语言replace函数用法 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/b/1713364413a630978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论