#include
#include
void inserting(char *p1,char *p2,int position)
{
char * ptmp=&p1[position]; /*此时ptmp临时指针指向串"son"*/
strcat(p2,ptmp); /*将ptmp(son)串接到串p2(per)后*/
p1[position]='\0'; /*让字符s那个位置置'\0'结束符*/
strcat(p1,p2); /*将p2(person)接到p1指向的串*/
printf("%s\n",p1);
}
void main()
{
char *p="the wrong son";
char *p2="per";
inserting(p,p2,10);
}
字符数组我试过,会引起内存问题。所以传指针咯。
其实按理来说,可以用字符数组,然后找几个临时字符数组保存咯(道理一样,strcat可以传字符数组的数组名)。
char* insertstring(char a[],char b[],int n)
{
if(n>strlen(a))
{
printf("'n'is too large!");
return(0);
}
char *h = (char*)malloc(100);
int i=0; int n_=n;
for(i=0;i
if(i
else{h[i]=a[n_];n_++;}
}
h[i]='\0';
return(h);
}
char *insertstring(char dst[], char src[], int pos)
{
int i = 0, j =0, k = 0;
char *temp = (char*)malloc(strlen(dst) + strlen(src) + 1);
while(i < pos)
{
temp[j++] = dst[i++];
}
while(k < strlen(src))
{
temp[j++] = src[k++];
}
while(i < strlen(dst))
{
temp[j++] = dst[i++];
}
temp[j] = '\0';
return temp;
}