#include<stdlib.h>
#define null 0
#define overflow 0
#define ok 1
typedef struct{
char *ch;
int length;
}hstring;
int strassign(hstring *t,char *chars)
{ int i,j;char *c;
if(t->ch) free(t->ch);
for(i=0,c=chars;c;++i,++c)
if(!i){t->ch=null;t->length=0;}
else {if(!(t->ch=(char *)malloc(i*sizeof(char))))
exit(overflow);
for(j=0;j<=i;j++)
t->ch[j]=chars[j];
t->length=i;
}
return ok;
}
int strlength(hstring *t)
{ return t->length;}
int strcompare(hstring *s,hstring *t)
{ int i,j;
for(i=0;i<s->length&&i<t->length;++i)
if(s->ch[i]!=t->ch[i])
return s->ch[i]-t->ch[i];
return s->length-t->length;
}
int concat(hstring *t,hstring *s1,hstring *s2)
{ int i,j;
if(!(t->ch)) free(t->ch);
if(!(t->ch=(char *)malloc((s1->length+s2->length)*sizeof(char))))
exit(overflow);
for(i=0;i<s1->length;i++)
t->ch[i]=s1->ch[i];
for(j=s1->length,i=0;j<(s1->length+s2->length)&&i<s2->length;j++,i++)
t->ch[j]=s2->ch[i];
return ok;
}
int substring(hstring *sub,hstring *s,int pos,int len)
{ int i,j;
if(!len)
{sub->ch=null;
sub->length=0;}
else {sub->ch=(char *)malloc(len*sizeof(char));
for(i=0,j=pos-1;i<len&&j<pos+len-1;i++,j++)
sub->ch[i]=s->ch[j];
sub->length=len;
}
return ok;
}
int index(hstring *s,hstring *t,int pos)
{ int i,j;
i=pos;j=0;
while(i<=s->length&&j<=t->length)
{ if( s->ch[i]==t->ch[j])
{ ++i;++j;}
else { i=i-j+2;j=0;}
}
if(j>t->length)
return i-t->length;
else return null;
}
void main()
{ hstring *t,*s1,*s2,*s3,*s;
int pos,len,length,h;
char *chars1="tangminghongwoshi";
char *chars2="zhongguoren";
char *chars3="minghong";
char *chars4="tangminghong";
pos=2;len=4;
strassign(t,chars1);
strassign(s1,chars4);
strassign(s3,chars3);
length=stlength(t);
printf("%d ",length);
printf("%d \n", strcompare(t,s1));
printf("\n");
if(concat(s,t,s1))
printf("%c",s->ch);
if(substring(s2,s,pos,len))
printf("%c",s2->ch);
h=index(t,s3,4);
printf("%d",h);
}