这是关于字符串的一系列基本操作
说明:程序中包含字符串的初始化,字符串的连接,字符串的赋值,字符串的逆置,字符串的删除以及插入操作
还有关于字符串的匹配问题,本程序采用BF蛮力算法来匹配字符串,也可以使用
KMP算法,具体算法并未在本 程序体现,在重要的位置处都有详细的代码注释
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 30
typedef struct //创建一个结构体
{
char *ch;
int length;
}Hstring;
//初始化一个字符链表
void Init_Hstring(Hstring *S)
{
S->ch = NULL;
S->length = 0;
}
//计算字符串长度
int LengthString(char *ch)
{
int i=0;
while (ch[i]!='\0')
i++;
return i;
}
//字符串赋值操作
int AssignHtring(Hstring *S, char *ch,int len) //将字符串ch的值赋值给结构体变量S
{
int i;
S->ch = (char *)malloc(sizeof(char)*(len + 1));
if (S->ch == NULL) return 0;//开辟空间失败!
for (i = 1; i <= len; i++)
S->ch[i] = ch[i-1];
S->length = len;
return 1;
}
//字符串的插入操作
int InsertString(Hstring *T, Hstring *S, int pos) //将字串T插入主串S的第pos前的位置,主串长度为Len
{
int i; char *Temp;
if (T->ch == NULL || S->ch == NULL || T == NULL || S == NULL || pos<1 || pos>S->length)
return 0; //以上条件成立,则不能进行插入操作
Temp = (char*)malloc(sizeof(char)*(S->length + T->length + 1));
if (Temp == NULL) return 0; //开辟空间失败!
for (i = 1; i < pos; i++)
Temp[i] = S->ch[i];
for (i = pos; i < pos + T->length; i++)
Temp[i] = T->ch[i - pos + 1];
for (i = pos + T->length; i <= S->length + T->length; i++)
Temp[i] = S->ch[i - T->length];
free(S->ch);
S->ch = Temp;
S->length = S->length + T->length;
return 1;
}
//字符串的连接操作
int Strcat_String(Hstring *S,Hstring *T)
{
int i;
if (S == NULL || S->ch == NULL || T->ch == NULL || T == NULL)
return 0;
S->ch = (char*)realloc(S->ch, sizeof(char)*(S->length + T->length + 1));
if (S->ch == NULL) return 0;
for (i = S->length + 1; i < S->length + T->length + 1; i++)
S->ch[i] = T->ch[i - S->length];
S->length = S->length + T->length;
return 1;
}
//字符串匹配函数
int Matching_String(Hstring*S, Hstring *T)
{
int i = 1, j = 1;
while (i <= S->length&&j <= T->length)
if (S->ch[i] == T->ch[j])
{
i++, j++;
}
else
{
i = i - j + 2;
j = 1;
}
if (j > T->length)
return i - T->length;
else return 0;
}
//字符串的删除操作
int Delete_String(Hstring *S, int pos, int len) //删除字符串S从第pos个位置开始连续len个字符
{
int i; char *temp;
if (S->ch == NULL || pos<1 || pos>S->length - len + 1)
return 0;
temp = (char*)malloc(sizeof(char)*(S->length - len + 1));
if (temp == NULL) return 0;
for (i = 1; i < pos; i++)
temp[i]=S->ch[i];
for (i = pos; i <= S->length - len; i++)
temp[i] = S->ch[i + len];
free(S->ch);
S->ch = temp;
S->length = S->length - len;
return 1;
}
//字符串的替换操作
int replace_Hstring(Hstring *S, Hstring *T, Hstring *V) //将主串S中与T字符串相等的字串替换为字符串V的内容
{
int pos;
while (Matching_String(S, T))
{
pos = Matching_String(S, T);
Delete_String(S, pos,T->length);
InsertString(V, S, pos);
}
return 1;//操作成功!
}
//求字符串的逆串
int StrReverse(Hstring *S,Hstring *T)//将字符串S中的元素逆置并且赋值给字符串T
{
int i;
if (S->ch == NULL || S == NULL)
return 0; //无法操作
T->ch = (char*)malloc(sizeof(char)*S->length);
if (T->ch == NULL) return 0; //开辟内存失败!
for (i = 1; i <=S->length; i++)
T->ch[S->length - i + 1] = S->ch[i];
T->length =S->length;
return 1;
}
//输出字符串函数
Print_String(Hstring *s)
{
int i = 1;
if (s->ch == NULL || s == NULL) return 0;
for (i = 1; i <= s->length; i++)
putchar(*(s->ch + i));
putchar('\n');
return 1;
}
//进入主函数main()
int main()
{
Hstring *S, *T, *V,*Q,q,s,t,v;
S = &s, T = &t; V = &v; Q = &q;
char ch1[MAXSIZE], ch2[MAXSIZE], ch3[MAXSIZE];
int len1, len2, len3;
printf("请输入主串S的值:\n");
scanf("%s",ch1);
printf("请输入模式串T的值:\n");
scanf("%s", ch2);
printf("请输入字符串V的值:\n");
scanf("%s",ch3);
Init_Hstring(S);
Init_Hstring(T);
Init_Hstring(V);
len1 = LengthString(ch1);
len2 = LengthString(ch2);
len3 = LengthString(ch3);
AssignHtring(S, ch1, len1);
AssignHtring(T, ch2, len2);
AssignHtring(V, ch3, len3);
printf("得到主串S的长度:%d,模式串T的长度为:%d,替换串V的长度为:%d\n",S->length,T->length,V->length);
//逆置字符串
StrReverse(S, Q);
printf("将主串S的字符逆置:\n");
Print_String(Q);
//将字符串查找并做替换
printf("将字符串S中所有与T相等的字符替换为V的字符操作:\n");
replace_Hstring(S, T, V);
Print_String(S);
//连接主串S与字符串T
printf("将字符串T的字符连接在主串S之后:\n");
Strcat_String(S, T);
Print_String(S);
}