串的定长顺序存储(部分代码)
连接两个串:
串的第一个空间存储串长
#define MIXSIZE 100
typedef int Status;
typedef char SString[MIXSIZE+1];
Status Concat(SString *s3,SString s1,SString s2)
{
if(s1[0]+s2[0]<=MIXSIZE)
{
printf("fgsd\n");
for(int i=1;i<=s1[0];i++)
{
*s3[i]=s1[i];
}
for(int i=s1[0]+1;i<s1[0]+s2[0];i++)
{
*s3[i]=s2[0];
}
*s3[0]=s1[0]+s2[0];
return TRUE;
}
else if(s1[0]<MIXSIZE&&(s1[0]+s2[0]>MIXSIZE))
{
printf("fgsd\n");
for(int i=1;i<=s1[0];i++)
{
*s3[i]=s1[i];
}
for(int i=s1[0]+1,j=1;(void)(i<=MIXSIZE),j<=MIXSIZE-s1[0];j++,i++)
{
*s3[i]=s2[j];
}
*s3[0]=MIXSIZE;
return FALSE;
}
else
{
for(int i=1;i<=MIXSIZE;i++)
{
*s3[i]=s1[i];
}
*s3[0]=MIXSIZE;
return FALSE;
}
}
求子串:
void SubString(SString *s3,SString s1,int pos,int len)
{
if(pos<1||len>s1[0]||len<0||len>s1[0]-pos+1)
printf("非法操作!\n");
for(int i=1,j=pos;(void)(i<len),j<pos+len-1;i++,j++)
{
*s3[i]=s1[j];
}
*s3[0]=len;
}
串的堆分配存储表示
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct
{
char *ch;
int length;
}HString;
//将字符串chars复制到字符串T中
void StrAssign(HString *T,char *chars)
{
int len = 0;
while(*(chars+len)!='\0') //计算串的长度
{
len++;
}
if(len==0) //串chars为空的情况
{
T->ch=NULL;
T->length=0;
}
else
{
T->ch=(char *)malloc(len * sizeof(char));
for(int i=0;i<len;i++)
{
T->ch[i]=*(chars+i);
}
T->length=len;
}
}
//打印串T元素
void Print_str(HString *T )
{
int i=0;
while(i<T->length)
{
printf("%c",T->ch[i++]);
}
printf("\n");
}
//返回串长度
int StrLength(HString *T)
{
return T->length;
}
//比较两串
int StrCompare(HString *T,HString *S )
{
int i;
if(T->length!=S->length)
{
if(T->length>S->length)
{
printf("字符串不等,s1的长度大于s2\n");
return 1;
}
else
{
printf("字符串不等,s1的长度小于s2\n");
return -1;
}
}
else
{
for(i=0;i<T->length;i++)
{
if(T->ch[i]>S->ch[i])
{
printf("长度相等,但s1>s2\n");
return 1;
}
else if(T->ch[i]<S->ch[i])
{
printf("长度相等,但s1>s2\n");
return -1;
}
}
printf("字符串相等\n");
return 0;
}
}
//连接两的字符串
void Concat(HString *T,HString *s1,HString *s2)
{
T->ch=(char *)malloc((s1->length+s2->length)*sizeof(char));
if(!T->ch)
exit(0);
for(int i=0;i<s1->length;i++)
{
T->ch[i]=s1->ch[i];
}
for(int i=s1->length,j=0;i<s1->length+s2->length;i++,j++)
{
T->ch[i]=s2->ch[j];
}
T->length=s1->length+s2->length;
}
//求子串
void SubString(HString *T,HString *S,int pos,int len)
{
T->ch=(char *)malloc(len*sizeof(char));
if(!T->ch)
exit(0);
for(int i=pos-1,j=0;i<pos+len;i++,j++)
{
T->ch[j]=S->ch[i];
}
T->length=len;
}
//清空串
void ClearString(HString *T )
{
if(T->ch)
{
free(T->ch);T->ch=NULL;
}
T->length=0;
}
//主函数,可对函数进行测试
int main()
{
char s1[100];
char s2[100];
printf(" 请输入字符串s1:\n");
gets(s1);
printf("请输入字符串s2:\n");
gets(s2);
HString S,S1,S2,*p,*p1,*p2;
p=&S; p1=&S1; p2=&S2;
StrAssign(p1, s1);
//StrAssign(p2, s2);
//StrCompare(p1, p2);
//Concat(p, p1, p2);
//SubString(p, p1, 2, 4);
//Print_str(p1);
//ClearString(p1);
//Print_str(p1);
}
串的模式匹配算法
1、传统算法
int Index(HString *T,HString *S, int pos)
{
int j=0;
while(pos<T->length&&j<S->length)
{
if(T->ch[pos]==S->ch[j])
{
pos++;j++;
}
else
{
pos=pos-j+2;
j=1;
}
}
if(j>=S->length)
{
int len;
len=pos-S->length+1;
printf("%d",len);
return len;
}
else
{
return 0;
}
}