用 c 写一个字符串替换
时间: 2023-05-21 14:05:13 浏览: 64
为了替换字符串,我们可以使用C语言中的字符串函数,例如strcpy()和strcat()。下面是一个示例代码,它使用strcpy()和strcat()函数来替换字符串中的子串:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100] = "hello world";
char old[10] = "world";
char new[10] = "Coder";
char *pos = strstr(str, old);
if (pos != NULL)
{
char tmp[100];
strcpy(tmp, pos + strlen(old));
strcpy(pos, new);
strcat(pos, tmp);
}
printf("%s", str);
return 0;
}
上述代码将字符串"hello world"中的"world"替换为"Coder",输出结果为"hello Coder"。
相关问题
怎么用C语言写一个字符串替换
以下是一种用C语言实现字符串替换的方法:
```c
#include <stdio.h>
#include <string.h>
void replace(char *str, char *old, char *new){
char *pos, temp[1000];
int index = 0;
int oldLen = strlen(old);
int newLen = strlen(new);
// 在字符串中找到需要替换的子串并替换
while ((pos = strstr(str, old)) != NULL){
// 将pos前面的字符串复制到temp中
strncpy(temp + index, str, pos - str);
index += pos - str;
// 将新的字符串复制到temp中
strncpy(temp + index, new, newLen);
index += newLen;
// 更新str指针
str = pos + oldLen;
}
// 将剩余的字符串复制到temp中
strcpy(temp + index, str);
// 将temp中的字符串复制回原字符串
strcpy(str, temp);
}
int main(){
char str[1000] = "hello world, world";
char old[10] = "world";
char new[10] = "cat";
replace(str, old, new);
printf("%s", str);
return 0;
}
```
上面的代码中,replace函数用于替换一个字符串中的指定子串。函数中使用了strstr函数来找到需要替换的子串的位置,然后使用strncpy函数将需要替换的子串前面的字符串和新的字符串复制到一个临时数组中,最后使用strcpy函数将临时数组中的字符串复制回原字符串。在主函数中,我们定义了一个字符串、一个需要替换的子串和一个新的字符串,并将它们作为参数传递给replace函数来进行替换。
用C语言编写一段字符串替换代码
以下是一段使用C语言编写的字符串替换代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100], old[20], new[20], tmp[100];
int i, j, k, old_len, new_len;
// 输入字符串和需要替换的子串和新子串
printf("请输入一段字符串:");
scanf("%[^\n]", str);
getchar();
printf("请输入需要替换的子串:");
scanf("%s", old);
printf("请输入新的子串:");
scanf("%s", new);
old_len = strlen(old);
new_len = strlen(new);
// 遍历字符串,查找需要替换的子串
for(i = 0; str[i] != '\0';) {
j = 0;
// 检查当前位置是否为需要替换的子串的起始位置
while (str[i + j] == old[j] && old[j] != '\0') {
j++;
}
// 如果找到了需要替换的子串,将其替换成新子串
if (j == old_len) {
for (k = 0; k < new_len; k++) {
tmp[i + k] = new[k];
}
i += new_len;
} else {
// 如果没有找到需要替换的子串,将此字符复制到临时字符串中
tmp[i] = str[i];
i++;
}
}
// 将临时字符串中的内容拷贝回原字符串
tmp[i] = '\0';
strcpy(str, tmp);
printf("替换后的字符串为:%s", str);
return 0;
}
```
该代码通过遍历输入的字符串,查找需要替换的子串,将其替换成新子串,并将替换后的字符串输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)