#define MAXLEN 50/*2009/09/18晚 写于白鹿原*/ typedef struct { char ch[MAXLEN]; int len; }SString; void StrAsign(SString *s)//串的建立函数 { int i,j; char c; printf("/nPlease input the length of String:"); scanf("%d",&j); getchar(); printf("/nplease input the value of String:"); for(i=0;i<j;i++) { scanf("%c",&c); s->ch[i]=c; } s->len=j; } void output(SString *s)//串的输出 { int i; for (i=0;i<s->len;i++) printf("%c",s->ch[i]); printf("/n"); } int StrInsert(SString *s,int pos,SString t) //串的插入函数,在pos位置后// { int i; if(pos<0||pos>s->len) return 0; if(s->len+t.len<=MAXLEN) { for(i=s->len+t.len-1;i>=t.len+pos;i--) s->ch[i]=s->ch[i-t.len]; for(i=0;i<t.len;i++) s->ch[i+pos]=t.ch[i]; s->len=s->len+t.len; } else if(pos+t.len<=MAXLEN){ for(i=MAXLEN-1;i>t.len+pos-1;i--) s->ch[i]=s->ch[i-t.len]; for(i=0;i<t.len;i++) s->ch[i+pos]=t.ch[i]; s->len=MAXLEN; } else { for(i=0;i<MAXLEN-pos;i++) s->ch[i+pos]=t.ch[i]; s->len=MAXLEN; } return 1; } void main() { int pos,flag; SString *s1; SString s2; s1=(SString *)malloc(sizeof(SString));//取地址 s1->len=0; printf("/nNOW Please input the String1!"); StrAsign(s1); getchar(); printf("/nNOW Please input the String2!"); StrAsign(&s2); getchar(); printf("Input the value of pos(must pos<S1.Len):"); scanf("%d",&pos); flag=StrInsert(s1,pos,s2); if(flag=1) printf("/nThe operate is OK!"); else printf("/nThe operate is FALSE!"); printf("/nThe String is:"); output(s1); }