int insertString(String *s, char *str,int pos);//声明
int main()
{
init(&s, "HELLOWORLD");
int k = insertString(&s,"123",2);
cout<<k<<endl;
show(&s);// HE123LLOWORLD
return 0;
}
int insertString(String *s, char *str,int pos)
{
if(s->data==NULL) return ERROR;
if(pos<0||pos>=s->length) return ERROR;
int len = strlen(str);
if(s->length+len>s->maxlength)
{
s->data= (char*)realloc(s->data, sizeof(char)*(len+s->length+1));//多加的一个1
s->maxlength=len+s->length;//实际存过的最大的字符串的个数
}
for(int i=s->length;i>=pos;i--)
{
s->data[i+len] = s->data[i];
}
for(int i=0;i<=len-1;i++)//pos---pos+len-1:len个字符
s->data[pos+i] = str[i];
s->length+=len;
return SUCCESS;
}