#include<stdio.h>
#include<stdlib.h>
void Insert(char * substr,char * str,int index)
{
int les, j,i,subles;
les = strlen(str);
subles = strlen(substr);
if(index >= les)
memcpy(str+les,substr,subles+1);
else
{
//移动substr的字符串长度次
for(j=0;j<=subles-1;j++)
{
for(i=les+1+j;i>=index+j;i--)
*(str+i)= *(str+i-1);
}
//copy substr
memcpy(str+index,substr,subles);
}
}
main()
{
void *p = malloc(30);
memcpy(p,"ChinPYG",8);
Insert("suiyunonghen",p,4);//Insert("a",p,4);得ChinaPYG
puts(p);
} #include <iostream>
#include <string>
using namespace std;
int main()
{
char *p=new char ;
char * str="ChinPYG";
strcpy(p,str);
for (int i=10;i>=4;i--)
{
*(p+i)=*(p+i-1);
}
*(p+4)='a';
cout<<p;
} bool insert(char *str,int pos,char temp)
{
if( pos>=1 && pos<=strlen(str)+1 )
{
int i;
for ( i=strlen(str)-1;i>=pos-1;i-- )
{
*(str+i+1) = *(str+i);
}
*(str+pos-1) = temp;
return 1;
}
else
return 0;
}
void main(int n,char *p[])
{
char *str = NULL;
str = (char *)malloc( 10 );
memset( str, 0, 10 );
strcpy( str,"ChinPYG" );
printf("操作前:%s\n",str);
insert( str,5,'a');
printf("操作后:%s\n",str);
free(str);
} 用c实现的确很麻烦,
页:
1
[2]