1、基于顺序存储下串的相关操作以及串匹配模式算法的实现
#include<stdio.h>
#include<string.h>
#define MAXSTRLEN 20
#define u_char unsigned char
typedef u_char SString[MAXSTRLEN+1];
void InitString(SString S) //初始化字符串
{
S[0] = '\0';
}
void StrAssign(SString S,char *str) //赋值字符串操作
{
int len = strlen(str);
for(int i = 0;i<len;i++)
{
S[i] = str[i];
}
S[len] = '\0';
}
int StrLength(SString S) //求字符串长度
{
int len = 0;
while(*S != '\0')
{
len++;
S++;
}
return len;
}
void StrCopy(SString S,SString T) //复制串操作
{
int len = StrLength(T);
for(int i=0;i<len;i++)
{
S[i] = T[i];
}
S[len] = '\0';
}
bool StrEmpty(SString S) //字符串结构判空操作
{
return S[0] == '\0';
}
int StrCompare(SString S,SString T) //字符串比较操作
{
int result = 0;
while(*S != '\0' || *T != '\0')
{
result = *S - *T;
if(result != 0)
break;
S++;
T++;
}
if(result > 0)
result = 1;
else if(result <0)
result = -1;
return result;
}
void StrConcat(SString T,SString s1,SString s2) //字符串连接操作
{
int len1 = StrLength(s1);
int len2 = StrLength(s2);
int i;
/*
//此处代码较为繁琐,简化后代码在下方
if(len1+len2<MAXSTRLEN)
{
for(i=0;i<len1;i++)
{
T[i] = s1[i];
}
for(int j=0;j<len2;j++)
{
T[i+j] = s2[j];
}
T[len1+len2] = '\0';
}
else if (len1<MAXSTRLEN)
{
for(i=0;i<len1;i++)
{
T[i] = s1[i];
}
for(int j=0;j<MAXSTRLEN-len1;j++)
{
T[i+j] = s2[j];
}
T[MAXSTRLEN] = '\0';
}
else
{
for(int j=0;j<len1;++j)
{
T[j] = s1[j];
}
T[MAXSTRLEN] = '\0';
}
*/
//此处为上述代码简化后,减少了代码长度
int lenj = 0,length = 0;
if(len1+len2<MAXSTRLEN)
{
length = len1+len2;
lenj = len2;
}
else if(len1 < MAXSTRLEN){
length = MAXSTRLEN;
lenj = MAXSTRLEN-len1;
}
else{
length = len1;
lenj =0;
}
for(i=0;i<len1;i++)
{
T[i] = s1[i];
}
for(int j=0;j<lenj;j++)
{
T[i+j] = s2[j];
}
T[length] = '\0';
}
void SubString(SString S,SString sub,int pos,int len) //在S字符串中截取子串
{
//求子串
int s_len = StrLength(S);
if(pos < 0 || pos>=s_len || len <0 || len > s_len)
return ;
int j=pos;
for(int i=0;i<len;i++)
{
sub[i] = S[j+i];
}
sub[len] = '\0';
}
int StrIndex(SString S,SString T,int pos)
{
int i =pos;
int j =0;
while(S[i]!='\0'&&T[j]!='\0')
{
if(S[i]==T[j])
{
i++;
j++;
}else{
i = i-j+1;
j=0;
}
}
if(T[j] == '\0')
return i-j;
return -1;
}
//void StrReplace(SString S,SString T,SString V)
void StrInsert(SString S,int pos,SString T) //在字符串中某位置插入子串
{
int s_len = StrLength(S);
int t_len = StrLength(T);
int i;
/*
//
if(s_len+t_len<=MAXSTRLEN)
{
for(i=s_len-1;i>=pos;--i)
{
S[i+t_len] = S[i];
}
int j = pos;
for(i=0;i<t_len;++i)
{
S[j+i] = T[i];
}
S[s_len+t_len] = '\0';
}
else if(s_len<MAXSTRLEN)
{
t_len = MAXSTRLEN - s_len;
for(i=s_len-1;i>=pos;--i)
{
S[i+t_len] = S[i];
}
int j = pos;
for(i=0;i<t_len;++i)
{
S[j+i] = T[i];
}
S[s_len+t_len] = '\0';
}
*/
if(s_len+t_len<=MAXSTRLEN)
{
t_len = StrLength(T);
}
else if(s_len+t_len>MAXSTRLEN)
{
t_len = MAXSTRLEN - s_len;
}
for(i=s_len-1;i>=pos;--i)
{
S[i+t_len] = S[i];
}
int j = pos;
for(i=0;i<t_len;++i)
{
S[j+i] = T[i];
}
S[s_len+t_len] = '\0';
}
void StrDelete(SString S,int pos,int len) //删除字串
{
int s_len = StrLength(S);
if(pos+len>MAXSTRLEN)
{
printf("数据输入不合法!!!");
return;
}
for(int i = pos;i<s_len;i++)
{
S[i] = S[i+len];
}
S[s_len-len] = '\0';
}
void StrClear(SString S) //清空字符串
{
S[0] = '\0';
}
void PrintString(SString S) //输出字符串
{
printf("%s \n",S);
}
int main()
{
SString S;
InitString(S);
//SString T;
//InitString(T);
//char *str = "abcdrh";
StrAssign(S,"ababcababcab");
SString T;
InitString(T);
StrAssign(T,"abc");
int index = StrIndex(S,T,0);
printf("%d \n",index);
//StrAssign(S,"abcdefhjkl");
//删除子串操作
//StrDelete(S,2,3);
//PrintString(S);
//插入子串
//SString T;
//InitString(T);
//StrAssign(T,"jjj");
//StrInsert(S,1,T);
//PrintString(S);
//截取子串
//SString sub;
//InitString(sub);
//SubString(S,sub,4,2);
//PrintString(sub);
//连接两个字符串部分
//SString Y;
//InitString(Y);
//StrConcat(Y,S,T);
//PrintString(Y);
//比较字符串部分
//int res = StrCompare(S,T);
//printf("%d \n",res);
//PrintString(S);
//复制字符串部分
//StrCopy(T,S);
//PrintString(T);
return 0;
}