程序代码:
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <assert.h>
using namespace std;
char * insert(char *,char *,int);
int main(int argc, char* argv[])
{
char *s,*s1;
int i = 0;
printf("input the first string:\n"); //输入第一个字符串
getchar();
s = new char[stdin->_cnt + 1];
stdin->_base[stdin->_cnt] = '\0';
strcpy(s,stdin->_base);
printf("input the second string:\n"); //输入第二个字符串
fflush(stdin);
getchar();
s1 = new char[stdin->_cnt + 1];
stdin->_base[stdin->_cnt] = '\0';
strcpy(s1,stdin->_base);
fflush(stdin);
printf("s=%s s1=%s",s,s1);
printf("\ninput insert number:\n");//输入要插入的位置
scanf("%d",&i);
char *str = insert(s,s1,i);
printf("%s\n",str);
delete[] str;
delete[] s;
delete[] s1;
return 0;
}
char * insert(char *strDest,char *strSrc,int pos)
{
assert((strDest != NULL)&&(strSrc != NULL));
int strSize = strlen(strDest) + strlen(strSrc);
char * strTemp = new char[strSize + 1];
if(pos < strlen(strDest))
{
for(int i = 0; i < strSize ; ++i)
{
if(i < pos)
{
strTemp[i] = strDest[i];
}
else
if( pos <= i && i < (pos + strlen(strSrc)))
{
strTemp[i] = strSrc[i - pos];
}
else
{
strTemp[i] = strDest[i - strlen(strSrc)];
}
}
}
else
{
strcpy(strTemp,strDest);
strcat(strTemp,strSrc);
}
strTemp[strSize] = '\0';
return strTemp;
}