#include <stdio.h>
#include <stdlib.h>
/*定义单链表结构体*/
#define MAXSTRLEN 255
#define TRUE 1
#define FALSE 0
struct sstring
{
char *ch;
int length ;
};
void StrAssign (struct sstring *T,char x)//初始条件:chars 是串常量,操作结果:赋于串T的值为 chars。
{
if(T->ch==NULL)
{
T->ch = malloc(MAXSTRLEN*sizeof(char)+1);
T->length = 1;
T->ch[T->length-1] = x;
}
else
{
if(T->length == MAXSTRLEN+1)
{
printf("The Lord string is full, please quit!\n");
exit(1);
}
else
{
T->length++;
T->ch[T->length-1] = x;
}
}
return ;
}
void StrCopy (struct sstring *T, char s[])//初始条件:串 s 存在,操作结果:由串 s 复制得串 T。
{
int sum = T->length,end = 0,i = 0;
while(*s!='\0')
{
T->ch[i] = *s;
printf("%c",T->ch[i]);
i++;
s++;
end++;
}
T->length = end;
for(i = 0;i<T->length;i++)
{
T->ch[end+i] = '\0';
}
return ;
}
void DestroyString (struct sstring *T)//初始条件:串 T 存在,操作结果:串 T 被销毁。
{
if(T->ch!=NULL)
{
free(T->ch);
T->ch = NULL;
T->length = 0;
return ;
}
else
{
printf("The string as empty strings, quit!\n");
return ;
}
}
int StrEmpty (struct sstring *T) //初始条件:串 T 存在,操作结果:若 T 为空串,则返回 true,否则返回 false。
{
if(*(T->ch)!='\0')
{
return FALSE;
}
else
{
return TRUE;
}
}
int StrCompare (struct sstring *T, char s[]) //初始条件:串 T 和 S 存在,操作结果:若T>S,则返回值>0;若T=S,则返回值=0;若T<S,则返回值<0。
{
int sum = 0;
while(*s!='\0')
{
s++;
sum++;
}
if(T->length > sum)
{
return 1;
}
else if(T->length = sum)
{
return 0;
}
else
{
return -1;
}
}
int StrLength (struct sstring *T)//返回主串T元素
{
if(T->length>=1)
{
return T->length;
}
else
{
return 0;
}
}
void ClearString (struct sstring *T) //初始条件:串 S 存在,操作结果:将 S 清为空串。
{
if(T->ch!=NULL)
{
free(T->ch);
T->ch = NULL;
T->length = 0;
}
T->ch = malloc(MAXSTRLEN*sizeof(char)+1);
return ;
}
void Concat (struct sstring *T,char s1[], char s2[]) //初始条件:串 S1 和 S2 存在,操作结果:用 T 返回由 S1 和 S2 联接而成的新串。
{
int s1sum = 0,s2sum = 0;
int s1i = 0,s2i = 0;
T->length = 0;
if(*s1!='\0'&&*s2!='\0')
{
while(s1[s1sum]!='\0')
{
s1sum++;
}
s1sum++;
while(s2[s2sum]!='\0')
{
s2sum++;
}
s2sum++;
if((s1sum+s2sum)>MAXSTRLEN)
{
if(s1sum>s2sum)
{
while(s1[s1i]!='\0')
{
T->ch[s1i]=s1[s1i];
T->length++;
}
while(T->length!=MAXSTRLEN)
{
T->ch[s1i]=s2[s2i];
s1i++;
s2i++;
T->length++;
}
}
else if(s1sum<s2sum)
{
while(s1[s1i]!='\0')
{
T->ch[s1i]=s1[s1i];
T->length++;
}
while(T->length!=MAXSTRLEN)
{
T->ch[s1i]=s2[s2i];
s1i++;
s2i++;
T->length++;
}
}
else
{
while(s1[s1i]!='\0')
{
T->ch[s1i]=s1[s1i];
T->length++;
s1i++;
}
while(T->length!=MAXSTRLEN)
{
T->ch[s1i]=s2[s2i];
s1i++;
s2i++;
T->length++;
}
}
}
else
{
while(s1[s1i]!='\0')
{
T->ch[s1i]=s1[s1i];
T->length++;
s1i++;
}
while(s2[s2i]!='\0')
{
T->ch[s1i]=s2[s2i];
s1i++;
s2i++;
T->length++;
}
}
return ;
}
else
{
printf("The string as empty strings, quit!\n");
return ;
}
}
char* SubString (struct sstring T,char sum[],int pos,int len)//初始条件:串T存在,1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1,操作结果:用 Sub 返回串T的第 pos 个字符起长度为 len 的子串。
{
int com = 0;
if(*T.ch!='\0')
{
if(pos>=1&&pos<=T.length)
{
if(len>=0&&len<=T.length-pos+1)
{
while(com!=len)
{
*sum++ = T.ch[pos-1+com];
com ++;
}
return sum;
}
else
{
printf("please enter len is 0---%d\n",(T.length-pos+1));
return NULL;
}
}
else
{
printf("please enter pos is 0---%d\n",T.length);
return NULL;
}
}
else
{
printf("The string as empty strings, quit!\n");
return NULL;
}
}
int Index (struct sstring T,char s[]) //初始条件:串T和S存在,S 是非空串,1≤pos≤StrLength(S),操作结果:若主串T中存在和串S值相同的子串,则返回它在主串T中第pos个字符之后第一次出现的位置;否则函数值为0。
{
int sum = 0,pos = 1,i = 0,j=0,end = 1;
if(T.ch!=NULL&&*s!='\0')
{
while(s[i]!='\0')
{
i++;
sum ++;
}
i = 1;
while((T.length-pos+1)>=sum)
{
while(T.ch[pos-1]!=s[j])
{
end = pos++;
if(T.ch[pos]=='\0')
{
return 0;
}
}
if((T.length-pos+1)>=sum)
{
if(i==sum)
{
return pos;
}
while(T.ch[pos+i-1]==s[i])
{
if(i=sum)
{
return pos;
}
i++;
pos++;
}
}
i = 1;
end++;
pos = end;
}
return 0;
}
else
{
printf("The initial conditions are not met, quit\n");
exit(1);
}
return -1;
}
void Replace (struct sstring *T,char S[], char V[])//初始条件:串 T,S 和 V 存在,S 是非空串,操作结果:用V替换主串T中出现的所有与S相等的不重叠的子串。
{
int sum = 0,and = 0,end = 0;
int h1 = 0,h2 = 0;
if(!StrEmpty(T)&&*S!='\0'&&*V!='\0')
{
while(S[and]!='\0')
{
and++;
}
while(V[end]!='\0')
{
end++;
}
sum = Index(*T,S);
while(sum!=0)
{
if(and>end)
{
while(h2!=end)
{
T->ch[sum+h1-1] = V[h2];
h1++;
h2++;
}
T->ch[sum+h2-(and-end)]=T->ch[sum+and-2];
}
else if(and<end)
{
while(h2!=and)
{
T->ch[sum+h1-1] = V[h2];
h1++;
h2++;
}
}
else
{
while(h2!=and)
{
T->ch[sum+h1-1] = V[h2];
h1++;
h2++;
}
}
sum = Index(*T,S);
}
}
return ;
}
void StrInsert (struct sstring *T,int pos, char S[]) //初始条件:串 T 和 S 存在,1≤pos≤StrLength(S)+1,操作结果:在串 T 的第 pos 个字符之前插入串 S。
{
int and =1,T1 = 0,S1 = 0;
if(!StrEmpty(T)&&*S!='\0')
{
if(pos>=1&&pos<=(T->length+1))
{
while(S[and-1]!='\0')
{
and++;
}
if(and>pos)
{
if(and+T->length<=MAXSTRLEN-1)
{
while(T1!=T->length)
{
T->ch[pos+T1] = T->ch[T1];
T1++;
}
while(S1!=pos)
{
T->ch[S1] = S[S1];
S1++;
}
}
else
{
printf("Situation go wrong!\n");
exit(1);
}
}
else if(and<pos)
{
if(and+T->length<=MAXSTRLEN-1)
{
while(T1!=T->length)
{
T->ch[and+T1] = T->ch[T1];
T1++;
}
while(S1!=and)
{
T->ch[S1] = S[S1];
S1++;
}
}
else
{
printf("Situation go wrong!\n");
exit(1);
}
}
else
{
if(and+T->length<=MAXSTRLEN-1)
{
while(T1!=T->length)
{
T->ch[and+T1] = T->ch[T1];
T1++;
}
while(S1!=and)
{
T->ch[S1] = S[S1];
S1++;
}
}
else
{
printf("Situation go wrong!\n");
exit(1);
}
}
}
else
{
printf("The initial conditions are not met, quit\n");
exit(1);
}
}
else
{
printf("The initial conditions are not met, quit\n");
exit(1);
}
}
void StrDelete (struct sstring *T,int pos, int len)//初始条件:串T 存在,1≤pos≤StrLength(S)-len+1,操作结果:从串 T中删除第 pos 个字符起长度为 len 的子串。
{
int i = 0,j = 0;
if(pos>=1&&pos<=T->length-len+1)
{
while(i!=len)
{
T->ch[pos-1+i] = '\0';
}
while((pos+len+j)!=T->length)
{
T->ch[pos-1+j] = T->ch[pos+len+j-1];
j++;
}
}
else
{
printf("The initial conditions are not met, quit\n");
exit(1);
}
}
void main()
{
char s[MAXSTRLEN]="";
char v[MAXSTRLEN]="";
char secondary[MAXSTRLEN]="";
char transition ;
struct sstring T;
int choice = 0,length = 0,i = 0,pos = 0 ,len = 0;
T.ch = NULL;
T.length = 0;
while(1)
{
printf("-----------------串算法菜单-----------------\n");
printf("1->构造主串T(动态在末尾添加,如需情况请选4)\n");
printf("2->构造子串S(长度以最大的为准,如需清空请选4)\n");
printf("3->构造子串V(长度以最大的为准,如需清空请选4)\n");
printf("4->清空串选择\n");
printf("5->遍历串选择\n");
printf("6->返回串长度选择\n");
printf("7->判空串选择\n");
printf("8->复制子串S到主串T\n");
printf("9->连结子串S和子串V到主串T\n");
printf("10->返回主串T第pos位置len长度字符数组\n");
printf("11->比较子串S或子串V与主串T大小\n");
printf("12->定位子串S或子串V与主串T中第一次相等位置\n");
printf("13->用子串S或V替换与主串T相等子串V或S所有不重叠的地方\n");
printf("14->在主串T第pos位置之前插入子串S\n");
printf("15->删除主串T第pos位置后len长度的字符\n");
printf("-----------------串算法菜单-----------------\n");
printf("请选择操作:");
fflush(stdin);
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("请输入构造主串T长度:");
scanf("%d",&length);
printf("\n");
printf("请输入主串T元素:");
fflush(stdin);
for(i=0;i<length;i++)
{
scanf("%c",&transition);
StrAssign(&T,transition);
}
printf("主串字符录入成功!\n");
length = 0;
break;
case 2:
printf("请输入构造子串S长度:");
scanf("%d",&length);
printf("\n");
printf("请输入子串S元素:");
fflush(stdin);
for(i=0;i<length;i++)
{
scanf("%c",&transition);
s[i] = transition;
}
printf("子串字符录入成功!\n");
length = 0;
break;
case 3:
printf("请输入构造子串V长度:");
scanf("%d",&length);
printf("\n");
printf("请输入子串V元素:");
fflush(stdin);
for(i=0;i<length;i++)
{
scanf("%c",&transition);
v[i] = transition;
}
printf("子串字符录入成功!\n");
length = 0;
break;
case 4:
printf("请选择清空字符串代号T->主串,S->子串,V->子串,其他->清空所有子串:");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'T':case 't':
ClearString(&T);
if(!StrEmpty(&T))
{
printf("主串T成功清空!");
getchar();
getchar();
}
else
{
printf("主串T清空失败!");
getchar();
getchar();
}
break;
case 'S':case 's':
choice = 0;
while(s[choice]!='\0')
{
s[choice] = '\0';
choice++;
}
printf("子串S成功清空!");
getchar();
getchar();
break;
case 'V':case 'v':
choice = 0;
while(v[choice]!='\0')
{
v[choice] = '\0';
choice++;
}
printf("子串v成功清空!");
getchar();
getchar();
break;
default :
ClearString(&T);
if(!StrEmpty(&T))
{
printf("主串T成功清空!\n");
}
else
{
printf("主串T清空失败!\n");
}
choice = 0;
while(s[choice]!='\0')
{
s[choice] = '\0';
choice++;
}
printf("子串S成功清空!\n");
choice = 0;
while(v[choice]!='\0')
{
v[choice] = '\0';
choice++;
}
printf("子串v成功清空!\n");
getchar();
getchar();
break;
}
break;
case 5:
printf("请选择遍历字符串代号T->主串,S->子串,V->子串,其他->遍历所有子串:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'T':case 't':
choice = 0;
printf("遍历主串T:");
while(choice!=T.length)
{
printf("%c",T.ch[choice]);
choice++;
}
getchar();
getchar();
break;
case 'S':case 's':
choice = 0;
printf("遍历子串S:");
while(s[choice]!='\0')
{
printf("%c",s[choice]);
choice++;
}
getchar();
getchar();
break;
case 'V':case 'v':
choice = 0;
printf("遍历子串V:");
while(v[choice]!='\0')
{
printf("%c",v[choice]);
choice++;
}
getchar();
getchar();
break;
default :
choice = 0;
printf("遍历主串T:");
while(choice!=T.length)
{
printf("%c",T.ch[choice]);
choice++;
}
printf("\n");
choice = 0;
printf("遍历子串S:");
while(s[choice]!='\0')
{
printf("%c",s[choice]);
choice++;
}
printf("\n");
choice = 0;
printf("遍历子串V:");
while(v[choice]!='\0')
{
printf("%c",v[choice]);
choice++;
}
getchar();
getchar();
break;
}
break;
case 6:
printf("请选择返回字符串长度代号T->主串,S->子串,V->子串,其他->返回所有子串长度:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'T':case 't':
printf("主串T长度为:%d",T.length);
getchar();
getchar();
break;
case 'S':case 's':
choice = 0;
while(s[choice]!='\0')
{
choice++;
}
printf("子串S长度为:%d",choice);
getchar();
getchar();
break;
case 'V':case 'v':
choice = 0;
while(v[choice]!='\0')
{
choice++;
}
printf("子串V长度为:%d",choice);
getchar();
getchar();
break;
default :
printf("主串T长度为:%d",T.length);
printf("\n");
choice = 0;
while(s[choice]!='\0')
{
choice++;
}
printf("子串S长度为:%d",choice);
printf("\n");
choice = 0;
while(v[choice]!='\0')
{
choice++;
}
printf("子串V长度为:%d",choice);
getchar();
getchar();
break;
}
break;
case 7:
printf("请选择判空字符串长度代号T->主串,S->子串,V->子串,其他->判空所有子串:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'T':case 't':
if(T.length!=0)
{
printf("主串T非空!");
getchar();
getchar();
}
else
{
printf("主串T为空!");
}
break;
case 'S':case 's':
choice = 0;
while(s[choice]!='\0')
{
choice++;
}
if(choice!=0)
{
printf("子串S非空!");
getchar();
getchar();
}
else
{
printf("子串S为空!");
}
break;
case 'V':case 'v':
choice = 0;
while(v[choice]!='\0')
{
choice++;
}
if(choice!=0)
{
printf("子串V非空!");
getchar();
getchar();
}
else
{
printf("子串V为空!");
}
break;
default :
if(T.length!=0)
{
printf("主串T非空!");
}
else
{
printf("主串T为空!");
}
printf("\n");
choice = 0;
while(s[choice]!='\0')
{
choice++;
}
if(choice!=0)
{
printf("子串S非空!");
}
else
{
printf("子串S为空!");
}
printf("\n");
choice = 0;
while(v[choice]!='\0')
{
choice++;
}
if(choice!=0)
{
printf("子串V非空!");
}
else
{
printf("子串V为空!");
}
getchar();
getchar();
break;
}
break;
case 8:
printf("请选择返回字符串代号替换主串T,S->子串,V->子串,其他->错误:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'S':case 's':
StrCopy(&T,s);
printf("S子串成功赋值给主串T!\n");
getchar();
getchar();
break;
case 'V':case 'v':
StrCopy(&T,v);
printf("V子串成功赋值给主串T!\n");
getchar();
getchar();
break;
default :
printf("选择错误!\n");
break;
}
break;
case 9:
printf("请选择连结顺序代号替换主串T,S->S子串+V子串,V->V子串+S子串,其他->错误:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'S':case 's':
Concat(&T,s,v);
printf("S子串+V子串成功赋值给主串T!\n");
getchar();
getchar();
break;
case 'V':case 'v':
Concat(&T,v,s);
printf("V子串+S子串成功赋值给主串T!\n");
getchar();
getchar();
break;
default :
printf("选择错误!\n");
break;
}
break;
case 10:
choice = 0;
printf("请输入主串T第pos位置:");
scanf("%d",&pos);
printf("请输入主串T第pos位置第len长度:");
scanf("%d",&len);
SubString(T,secondary,pos,len);
printf("遍历主串T第%d处%dlen长度数组:",pos,len);
while(secondary[choice]!='\0')
{
printf("%c",secondary[choice]);
secondary[choice] = '\0';
choice++;
}
getchar();
getchar();
break;
case 11:
printf("请选择与主串T比较字符串代号,S->子串,V->子串,其他->错误:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'S':case 's':
if(StrCompare(&T,s)>0)
{
printf("主串T大于子串S!\n");
getchar();
getchar();
break;
}
else if(StrCompare(&T,s)<0)
{
printf("主串T小于子串S!\n");
getchar();
getchar();
break;
}
else
{
printf("主串T等于子串S!\n");
getchar();
getchar();
break;
}
case 'V':case 'v':
if(StrCompare(&T,v)>0)
{
printf("主串T大于子串V!\n");
getchar();
getchar();
break;
}
else if(StrCompare(&T,v)<0)
{
printf("主串T小于子串V!\n");
getchar();
getchar();
break;
}
else
{
printf("主串T等于子串V!\n");
getchar();
getchar();
break;
}
default :
printf("选择错误!\n");
break;
}
break;
case 12:
printf("请选择比较字符代号与主串T进行定位操作,S->S子串,V->V子串,其他->错误:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'S':case 's':
printf("S子串与主串T第一次相等的位置为:%d",Index(T,s));
getchar();
getchar();
break;
case 'V':case 'v':
printf("V子串与主串T第一次相等的位置为:%d",Index(T,v));
getchar();
getchar();
break;
default :
printf("选择错误!\n");
break;
}
break;
case 13:
printf("请选择替换字符代号与主串T进行覆盖操作,S->S覆盖V与T相等的字符,V->V覆盖S与T相等的字符,其他->错误:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'S':case 's':
Replace(&T,v,s);
printf("S覆盖V与T相等的字符成功!");
getchar();
getchar();
break;
case 'V':case 'v':
Replace(&T,s,v);
printf("V覆盖S与T相等的字符成功!");
getchar();
getchar();
break;
default :
printf("选择错误!\n");
break;
}
break;
case 14:
printf("请输入插入主串第pos位置:");
scanf("%d",pos);
printf("请选择字符代号与主串T进行插入操作,S->S子串,V->V子串,其他->错误:\n");
fflush(stdin);
scanf("%c",&transition);
switch(transition)
{
case 'S':case 's':
StrInsert(&T,pos,s);
printf("S插入T成功!");
getchar();
getchar();
break;
case 'V':case 'v':
StrInsert(&T,pos,s);
printf("V插入T成功!");
getchar();
getchar();
break;
default :
printf("选择错误!\n");
break;
}
break;
/*case 15:
default :*/
}
}
}