大话数据结构12 串String

基础内容

串的操作:
在这里插入图片描述
串的比较
在这里插入图片描述

代码

#include "string.h"
#include "stdio.h"    
#include "stdlib.h"   
#include "io.h"  
#include "math.h"  
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 40 /* 存储空间初始分配量 */

typedef int Status;		/* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int ElemType;	/* ElemType类型根据实际情况而定,这里假设为int */

typedef char String[MAXSIZE + 1]; //0号单元存放串的长度

//生成一个其值等于chars的串T
Status StrAssign(String T, const char* chars)
{
	int i;
	if (strlen(chars) > MAXSIZE)
		return ERROR;
	else
	{
		T[0] = strlen(chars)-1;//有一个空格
		for (int i = 1; i <= T[0]; i++)
			T[i] = *(chars + i - 1); //这一块要注意 容易出错
		return OK;
	}
}

//复制串S到T
Status StrCopy(String T, String S)
{
	for (int i = 0; i <= S[0]; i++)
	{
		T[i] = S[i];
	}
	return OK;
}

//判断S是否为空串
Status StrEmpty(String S)
{
	if (S[0] == 0)
		return TRUE;
	return FALSE;
}

/*  操作结果: 若S>T,则返回值>0;若S=T,则返回值=0;若S<T,则返回值<0 */
int StrCompare(String S, String T)
{
	for (int i = 1; i < S[0] && i < T[0]; i++)
	{
		if (S[i] != T[i])
			return S[i] - T[i];
		return S[0] - T[0];
	}
}

//返回串元素个数
int StrLength(String S)
{
	return S[0];
}

//将串清空
Status ClearString(String S)
{
	S[0] = 0;
	return OK;
}

//用T返回S1和S2 联结而成的新串
Status Concat(String T, String S1, String S2)
{
	int i;
	if (S1[0] + S2[0] <= MAXSIZE)//未截断
	{
		T[0] = S1[0] + S2[0];
		for (int i = 1; i <= S1[0]; i++)
		{
			T[i] = S1[i];
		}
		for (int i = 1; i <= S2[0]; i++)
		{
			T[i+S1[0]] = S2[i];
		}
		return TRUE;

	}
	else
	{//截断S2
		for (int i = 1; i <= S1[0]; i++)
		{
			T[i] = S1[i];
		}
		for (int i = 1; i <= MAXSIZE - S1[0]; i++)
		{
			T[i + S1[0]] = S2[i];
		}
		return FALSE;

	}
	return FALSE;
}
/* 用Sub返回串S的第pos个字符起长度为len的子串。 */
Status SubString(String sub, String S, int pos, int len)
{
	if ((S[0] - pos - len + 1) < 0 || pos<1 || pos>S[0]||len <0)
	{
		return ERROR;
	}
	for (int i = 1; i <= len; i++)
	{
		sub[i] = sub[pos + i - 1];
	}
	sub[0] = len;
	return OK;
}

//返回子串T在主串中第pos个字符之后的位置
//若不存在 则函数返回值为0
// 其中 T非空 1<=pos<=strLength(S)

int Index(String S, String T, int pos)
{
	int i = pos;
	int j = 1;
	while (i <= S[0] && j <= T[0])
	{
		if (S[i] == T[j])//匹配过程
		{
			++i;
			++j;
		}
		else//重新匹配
		{
			i = j + 2;
			j = 1;
		}
	}
	if (j > T[0])
		return i - T[0]; //返回位置
	return 0;
}
/*  T为非空串。若主串S中第pos个字符之后存在与T相等的子串, */
/*  则返回第一个这样的子串在S中的位置,否则返回0 */
int Index2(String S, String T, int pos)
{
	int n, m, i;
	String sub;
	if (pos > 0 && pos < S[0])
	{
		n = StrLength(S);
		m = StrLength(T);
		i = pos;
		while (i <= n-m+1)
		{
			SubString(sub, S, i, m);/* 取主串中第i个位置长度与T相等的子串给sub */
			if (StrCompare(sub, T) != 0)
				++i;
			else
				return i; //如果两串相等则返回位置
		}
	}
	return 0;
}

/*  初始条件: 串S和T存在,1≤pos≤StrLength(S)+1 */
/*  操作结果: 在串S的第pos个字符之前插入串T。完全插入返回TRUE,部分插入返回FALSE */
Status StrInsert(String S, int pos, String T)
{
	int i;
	if (pos < 1 || pos > S[0] + 1)
		return ERROR;
	if (S[0] + T[0] <= MAXSIZE)
	{
		//完全插入
		for (int i = S[0]; i >= pos; i--)//这种从尾巴开始移动的策略号
		{
			S[i + T[0]] = S[i];
		}
		for (int i = pos; i < pos + T[0]; i++)
		{
			S[i] = T[i - pos + 1];
		}
		S[0] = S[0] + T[0];
		return TRUE;
	}
	else
	{
		//部分插入
		for (int i = MAXSIZE; i <= pos; i--)
		{
			S[i] = S[i+S[0]-MAXSIZE];//空余空间插满
		}
		for (int i = pos; i < pos + MAXSIZE -S[0];i++)
		{
			S[i] = T[i - pos + 1];
		}
		S[0] = MAXSIZE;
		return FALSE;
	}
}

/*  初始条件: 串S存在,1≤pos≤StrLength(S)-len+1 */
/*  操作结果: 从串S中删除第pos个字符起长度为len的子串 */
Status strDelete(String S, int pos, int len)
{
	int i;
	if (pos < 1 || pos > S[0] - len + 1 || len < 0)
		return ERROR;
	for (int i = pos + len; i <= S[0]; i++)
	{
		S[i - len] = S[i];
	}
	S[0] = S[0] - len;
	return OK;
}

/*  初始条件: 串S,T和V存在,T是非空串(此函数与串的存储结构无关) */
/*  操作结果: 用V替换主串S中出现的所有与T相等的不重叠的子串 */

Status Replace(String S, String T, String V)
{
	int j = 1;
	if (StrEmpty(T))
		return ERROR;
	do
	{
		j = Index(S, T, j);
		if (j)
		{
			strDelete(S, j, StrLength(T)); /*  删除该串T */
			StrInsert(S, j, V);/*  在原串T的位置插入串V */
			j += StrLength(V);/*  在插入的串V后面继续查找串T */
		}
	} while (j);
	return OK;
}

/*  输出字符串T */
void StrPrint(String T)
{
	int i;
	for (i = 1; i <= T[0]; i++)
		printf("%c", T[i]);
	printf("\n");
}

int main()
{
	int i, j;
	Status k;
	char s;
	String t, s1, s2;
	printf("请输入串s1: ");

	k = StrAssign(s1, "abcd "); //注意这里留一个空格
	if (!k)
	{
		printf("串长超过MAXSIZE(=%d)\n", MAXSIZE);
		exit(0);
	}
	printf("串长为%d 串空否?%d(1:是 0:否)\n", StrLength(s1), StrEmpty(s1));
	StrCopy(s2, s1);
	printf("拷贝s1生成的串为: ");
	StrPrint(s2);

	printf("请输入串s2: ");

	k = StrAssign(s2, "efghijk ");
	if (!k)
	{
		printf("串长超过MAXSIZE(%d)\n", MAXSIZE);
		exit(0);
	}
	i = StrCompare(s1, s2);
	if (i < 0)
		s = '<';
	else if (i == 0)
		s = '=';
	else
		s = '>';
	printf("串s1%c串s2\n", s);

	k = Concat(t, s1, s2);
	printf("串s1联接串s2得到的串t为: ");
	StrPrint(t);

	if (k == FALSE)
		printf("串t有截断\n");
	ClearString(s1);
	printf("清为空串后,串s1为: ");
	StrPrint(s1);
	printf("串长为%d 串空否?%d(1:是 0:否)\n", StrLength(s1), StrEmpty(s1));

	printf("求串t的子串,请输入子串的起始位置,子串长度: ");
	i = 2;
	j = 3;
	printf("%d,%d \n", i, j);
	k = SubString(s2, t, i, j);
	if (k)
	{
		printf("子串s2为: ");
		StrPrint(s2);
	}

	printf("从串t的第pos个字符起,删除len个字符,请输入pos,len: ");
	i = 4;
	j = 2;
	printf("%d,%d \n", i, j);
	strDelete(t, i, j);
	printf("删除后的串t为: ");
	StrPrint(t);

	i = StrLength(s2) / 2;
	StrInsert(s2, i, t);
	printf("在串s2的第%d个字符之前插入串t后,串s2为:\n", i);
	StrPrint(s2);

	i = Index(s2, t, 1);
	printf("s2的第%d个字母起和t第一次匹配\n", i);
	SubString(t, s2, 1, 1);
	printf("串t为:");
	StrPrint(t);
	Concat(s1, t, t);
	printf("串s1为:");
	StrPrint(s1);
	Replace(s2, t, s1);
	printf("用串s1取代串s2中和串t相同的不重叠的串后,串s2为: ");
	StrPrint(s2);


	return 0;


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值