题目:用你所熟悉的任意一种程序语言,编写一个完整的过程,将一个字符串插入到另一个字符串的某个位置后面(例如:将“abc”插入到“abcdef”的第三个字符位置后面,结果为“abcabcdef”)。编写程序时,请在必要的地方加以注释(注:不能用该程序语言的内置函数或过程)。 char* insert(char *dest,char *src,int n){ char *end = dest; char *subend = src; while(*end++ != '\0');//find the end of the string while(*subend++ != '\0');//find the end of the string int m = subend-src-1;//strlen of src while(end>=dest+n)//remove the chars in dest { *(end+m) = *end; end--; } end++; while(*src != '\0')//insert src into dest { *end++ = *src++; } return dest;}int main(){ char a[10]="12345"; char b[3]="ab"; printf("%s\n",insert(a,b,2)); cin.get(); return 0;}

评论