Lupkid 发表于 2007-10-20 11:55

[求助]大家看看

<P>//----串的堆分配存储--------<BR>typedef struct{<BR>char *ch;<BR>int length;<BR>}HString;<BR>Status StrInsert(HString &amp;S,int pos.HStirng T){<BR>//1&lt;=pos&lt;=StrLength(S)+1. 在串S的第pos个字符之前插入串T.<BR>if(pos&lt;1||pos&gt;S.length+1) return ERROR;    //pos不合法<BR>if(T.length){                              //T非空,则重新分配空间,插入T<BR>  if(!(S.ch=(char *)realloc(S.ch,(S.length+T.length)*sizeof(char)))) exit(OVERFLOW);<BR><FONT color=#ff0000>  for(i=S.length-1;i&gt;=pos-1;--i)  //为插入T而腾出位置<BR>    S.ch[i+T.length]=S.ch[i];</FONT><BR>  S.ch[pos-1..pos+T.length-2]=T.ch[0..T.length-1];  //插入T<BR>  S.length+=T.length;<BR>}<BR>return OK;<BR>}<BR><BR><BR>这句好像没用啊</P>

nuciewth 发表于 2007-10-20 12:06

for(i=S.length-1;i&gt;=pos-1;--i)  //为插入T而腾出位置<BR>    S.ch[i+T.length]=S.ch[i];<BR><BR>  S.ch[pos-1..pos+T.length-2]=T.ch[0..T.length-1];  //插入T,<FONT color=#ff0000>这里也是一层循环,上面是为这个一步挪出空间的.</FONT><BR>

Lupkid 发表于 2007-10-20 12:15

<P>我明白了,我没看清楚,原来是在S的第pos个字符前插入<BR>谢谢</P>

页: [1]

编程论坛