将字符串t插入到字符串s中,在位置pos后插入。不得使用字符串操作函数,输出组合成的字符串。c语言
时间: 2024-05-09 10:14:32 浏览: 9
可以使用字符数组来实现字符串的插入操作,具体实现如下:
```c
#include <stdio.h>
void strInsert(char s[], char t[], int pos) {
int i, j;
// 求出字符串s和t的长度
for (i = 0; s[i] != '\0'; i++);
for (j = 0; t[j] != '\0'; j++);
// 将s中pos后面的字符向后移动t的长度个位置
for (i = i - 1; i >= pos; i--) {
s[i + j] = s[i];
}
// 将t插入到s中pos位置后面
for (i = 0; i < j; i++) {
s[pos + i] = t[i];
}
// 输出组合后的字符串
printf("%s", s);
}
int main() {
char s[100] = "hello, world!";
char t[100] = "CSDN";
int pos = 7;
strInsert(s, t, pos);
return 0;
}
```
在上面的代码中,`strInsert`函数用于将字符串`t`插入到字符串`s`中,插入位置为`pos`。首先求出字符串`s`和`t`的长度,然后将`s`中`pos`后面的字符向后移动`t`的长度个位置,最后将`t`插入到`s`中`pos`位置后面。最后输出组合后的字符串。在`main`函数中,我们定义了两个字符串`s`和`t`,以及插入位置`pos`,然后调用`strInsert`函数进行字符串插入操作。