C语言程序设计实例31
把数组s2中的所有字符插入到s1的第n个字符后面。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *insert(char *s1, char *s2, int n)
{
char *p1;
int i, len1, len2;
len1 = strlen(s1);
len2 = strlen(s2);
p1 = s1;
for (i = 0; i < len1 - n; i++)
{
*(s1 + n + len2 + i) = *(s1 + n + i);
}
i = 0;
while (*s2)
{
*(s1 + i + n) = *s2;
i++;
s2++;
}
*(p1 + len1 + len2) = '\0';
return p1;
}
int main()
{
char s1[50], s2[10], *p;
int n;
int i;
printf("s1=");
scanf("%s", s1);
printf("s2=");
scanf("%s", s2);
printf("n=");
scanf("%d", &n);
if (n < strlen(s1))
{
p = insert(s1, s2, n);
}
else
{
printf("error:n>strlen(s1)\n");
}
puts(p);
system("pause");
return 0;
}