请用c语言写一个串插入另一个串的函数
时间: 2023-06-11 09:07:21 浏览: 47
下面是用C语言实现将一个字符串插入到另一个字符串中指定位置的函数:
```c
#include <stdio.h>
#include <string.h>
void insert(char *str1, char *str2, int pos) {
int len1 = strlen(str1);
int len2 = strlen(str2);
int i;
// 将str1中pos位置后的字符向后移动len2个位置
for (i = len1; i >= pos; i--) {
str1[i+len2] = str1[i];
}
// 将str2插入到str1中pos位置
for (i = 0; i < len2; i++) {
str1[pos+i] = str2[i];
}
}
int main() {
char str1[100] = "hello world";
char str2[] = "beautiful ";
int pos = 6;
printf("Before insert: %s\n", str1);
insert(str1, str2, pos);
printf("After insert: %s\n", str1);
return 0;
}
```
输出结果为:
```
Before insert: hello world
After insert: hello beautiful world
```
其中,insert函数接受三个参数:要插入的字符串(str1)、要被插入的字符串(str2)和插入的位置(pos),并将str2插入到str1的pos位置。实现的过程是先将str1中pos位置后的字符向后移动len2个位置,然后再将str2插入到str1中pos位置。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)