C字符串插⼊函数char* strins(char* dest, const char* src, int pos)
{
int len = strlen(src);
for (int i = strlen(dest); i >= pos; i--)
dest[i + len] = dest[i];    // 同时也拷贝字符串结束符
for (int j = pos; j < pos + len; j++)
dest[j] = src[j - pos];
字符串函数注册登录return dest;
}
另⼀种⽅法:
void insert(char *s, char *t, int i)
{
char *q = t;
char *p = s;
if (q == NULL) return;
while (*p != '\0')
{
if (0 >= --i)
{
memmove(p + strlen(t), p, strlen(p));
break;
}
p++;
}
while (*q != '\0')
{
*p = *q;
p++;
q++;
}
}
貌似再优化也需要2次循环,⼀次将插⼊位置后的⼦串后移,⼀次将新⼦串赋值到插⼊位置。需要注意的是内存溢出的问题,⽬标字符串必须有⾜够的空间。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。