【数据结构】C语言-实现(定长顺序存储)串的基本操作(无C++内容)

        本文章实现的基本操作:1.初始化;2.串的输入;3.串的输出;4.串的长度;5.串的比较;6.清空串;7.判空;8.串的连接;9.BF匹配模式;10.串的删除。

目录

头文件 SString.h

函数文件 Function.c

主函数测试部分 Main.c


头文件 SString.h

        头文件中,定义宏变量,使操作更加规范、直观;自定义类型名称以及串的结构体;声明函数文件内的函数。

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -1

#define MAXSIZE 255

typedef int Status;
typedef char Elemtype;

typedef struct
{
    char ch[MAXSIZE + 1];
    int length;
}SString;

//初始化
void InitString(SString* S);

//串的输入
Status PutString(SString* S, Elemtype c);

//串的输出
Status OutString(SString S);

//串的长度
int LengthString(SString S);

//串的比较
Status CompareString(SString* S, SString T);

//清空串
Status ClearString(SString* S);

//判空
Status isEmptyString(SString S);

//串的连接
SString Concat(SString S1, SString S2);

//模式匹配BF算法
int BFString(SString S, SString T, int pos);

//串的删除
Status DelString(SString* S, int pos, int len);

函数文件 Function.c

#include <stdio.h>
#include <stdlib.h>
#include "SString.h"

//初始化
void InitString(SString* S)
{
	S->length = 0;
}

//串的输入
Status PutString(SString* S, Elemtype c)
{
	if (S->length >= MAXSIZE)
	{
		return OVERFLOW;
	}
	S->ch[S->length + 1] = c;
	S->length++;
	return OK;
}

//串的输出
Status OutString(SString S)
{
	if (S.length == 0)
	{
		return ERROR;
	}
	int i = 1;
	while (i <= S.length)
	{
		printf("%c", S.ch[i]);
		i++;
	}
	printf("\n");
	return OK;
}

//串的长度
int LengthString(SString S)
{
	return S.length;
}

//串的比较
Status CompareString(SString S, SString T)
{
	if (T.length != S.length)
	{
		return ERROR;
	}
	int i = 1;
	while (i <= S.length)
	{
		if (S.ch[i] != T.ch[i])
		{
			return ERROR;
		}
		i++;
	}
	return OK;
}

//清空串
Status ClearString(SString* S)
{
	if (S->length == 0)
	{
		return ERROR;
	}
	S->length = 0;
	return OK;
}

//判空
Status isEmptyString(SString S)
{
	if (S.length == 0)
	{
		return OK;
	}
	return ERROR;
}

//串的连接
SString Concat(SString* S1, SString S2)
{
	int i = 1;
	while (i <= S2.length)
	{
	S1->ch[S1->length + 1] = S2.ch[i];
	S1->length++;
	i++;
	}
	return *S1;
}

//模式匹配BF算法
int BFString(SString S, SString T, int pos)
{
	if (pos<1 || pos>(S.length - T.length))
	{
		return 0;
	}
	int i, j;
	i = pos;//主串位置
	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 + 1))
	{
		return i - T.length;
	}
	else
	{
		return 0;
	}
}

//串的删除
Status DelString(SString* S, int pos, int len)
{
	if (S->length == 0)
	{
		return ERROR;
	}
	int i = pos + len;
	S->length = S->length - len;
	while (i <= S->length + len)
	{
		S->ch[pos] = S->ch[i];
		i++;
		pos++;
	}
	return OK;
}

主函数测试部分 Main.c

#include <stdio.h>
#include <stdlib.h>
#include "SString.h"

int main()
{
	SString S;
	InitString(&S);
	int select = 0;
	do
	{
		printf("1.输入串\n");
		printf("2.输出串\n");
		printf("3.串的长度\n");
		printf("4.串的比较\n");
		printf("5.清空串\n");
		printf("6.判空\n");
		printf("7.串的连接\n");
		printf("8.模式匹配\n");
		printf("9.串的删除\n");
		printf("0.退出\n");
		printf("输入选择:");
		scanf("%d", &select);
		switch (select)
		{
		case 1:
		{
			printf("请输入串:");
			getchar();
			char temp;
			while ((temp = getchar()) != '\n')
			{
				if (PutString(&S, temp) == OVERFLOW)
				{
					printf("内存已满!\n");
					break;
				}
			}
			printf("存储完成!\n");
			break;
		}
		case 2:
		{
			printf("当前串:");
			if (OutString(S) == ERROR)
			{
				printf("串为空!\n");
			}
			break;
		}
		case 3:
		{
			printf("串的长度为:%d\n", LengthString(S));
			break;
		}
		case 4:
		{
			SString T;
			InitString(&T);
			printf("请输入需要比较的串:");
			getchar();
			char temp;
			while ((temp = getchar()) != '\n')
			{
				if (PutString(&T, temp) == OVERFLOW)
				{
					printf("内存已满!\n");
					break;
				}
			}
			if (CompareString(S, T) == ERROR)
			{
				printf("串不相等!\n");
			}
			else
			{
				printf("串相等!\n");
			}
			break;
		}
		case 5:
		{
			if (ClearString(&S) == ERROR)
			{
				printf("串为空!\n");
			}
			else
			{
				printf("已清空!\n");
			}
			break;
		}
		case 6:
		{
			if (isEmptyString(S) == OK)
			{
				printf("串为空!\n");
			}
			else
			{
				printf("串不为空!\n");
			}
			break;
		}
		case 7:
		{
			SString S2;
			InitString(&S2);
			printf("请输入需要连接的串:");
			getchar();
			char temp;
			while ((temp = getchar()) != '\n')
			{
				if (PutString(&S2, temp) == OVERFLOW)
				{
					printf("内存已满!\n");
					break;
				}
			}
			SString T = Concat(&S, S2);
			printf("连接后的串:");
			if (OutString(T) == ERROR)
			{
				printf("串为空!\n");
			}
			break;
		}
		case 8:
		{
			printf("请输入匹配字符:");
			getchar();
			SString T;
			InitString(&T);
			char temp;
			while ((temp = getchar()) != '\n')
			{
				if (PutString(&T, temp) == OVERFLOW)
				{
					printf("内存已满!\n");
					break;
				}
			}
			printf("请输入匹配起始点:");
			int pos = 0;
			scanf("%d", &pos);
			int BF_temp = BFString(S, T, pos);
			if (BF_temp == 0)
			{
				printf("未查找到!\n");
			}
			else
			{
				printf("所在位置为:%d", BF_temp);
			}
			break;
		}
		case 9:
		{
			int pos = 0;
			int len = 0;
			printf("请输入删除的起始位置:");
			scanf("%d", &pos);
			printf("请输入删除长度:");
			scanf("%d", &len);
			if (DelString(&S, pos, len) == ERROR)
			{
				printf("串为空!\n");
			}
			else
			{
				printf("删除成功,删除后为:");
				OutString(S);
			}
			break;
		}
		case 0:
			break;
		default:
		{
			printf("输入有误!\n");
			break;
		}
		}
		printf("\n--------------------------\n\n");
	} while (select != 0);
	return 0;
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值