数据结构之字符串

  版权声明:本文参考了严蔚敏的《 数据结构》。未经作者允许,严禁用于商业出版,否则追究法律责任。网络转载请注明出处,这是对原创者的起码的尊重!!!


1 串的主要操作

计算串长、复制、比较、取子串、匹配

2 代码

#include<iostream>
using namespace std;
#define ok 1
#define error 0
#define yes 1
#define no 0
#define NULL 0
struct str //定义串,就是动态顺序表的定义
{
	char *ch;
	int length;
};
void init(str &str1)
{
	str1.ch = NULL;
	str1.length = 0;
}
int getlength(str str1)//取串的长度
{
	return str1.length;
}
int getarraylength(char str1[])//取数组的长度
{
	int len = 0;
	while (str1[len] != NULL)
		len++;
	return len;
}
int strcopy1(str str1, str &str2)//将str1复制给str2
{
	if (str2.ch != NULL)
	{
		free(str2.ch);
		str2.ch = NULL;
		str2.length = 0;
	}
	if (str1.length == 0)
	{
		str2.ch = NULL;
		str2.length = 0;
		return ok;
	}
	else
	{
		str2.ch = (char*)malloc(sizeof(char)*(str1.length + 1));
		if (str2.ch == NULL)
			return error;
		else
		{
			for (int i = 0; i <= str1.length; i++)//要把最后的"\0"复制过去
			{
				str2.ch[i] = str1.ch[i];
			}
			str2.length = str1.length;
			return ok;
		}
	}

}
int strcopy2(char str1[], str &str2)//将数组str1复制给str2
{
	if (str2.ch)
	{
		free(str2.ch);
		str2.ch = NULL;
		str2.length = 0;
	}
	int len = getarraylength(str1);
	if (len == 0)
	{
		str2.ch = NULL;
		str2.length = 0;
		return ok;
	}
	else
	{
		str2.ch = (char*)malloc(sizeof(char)*(len + 1));
		if (str2.ch == NULL)
			return error;
		else
		{
			for (int i = 0; i <= len; i++)//要把最后的"\0"复制过去
			{
				str2.ch[i] = str1[i];
			}
			str2.length = len;
			return ok;
		}
	}
}
int strcompare(str str1, str str2)//比较str1,str2的大小
{
	for (int i = 0; i < str1.length&&i < str2.length; i++)
		if (str1.ch[i] != str2.ch[i])
			return str1.ch[i] - str2.ch[i];
	return str1.length - str2.length;
}
int strcontact(str str1, str str2, str &str3)//将str2链接在str1后面
{
	int i,j;
	if (str3.ch != NULL)
	{
		free(str3.ch);
		str3.length = 0;
	}
	str3.ch = (char*)malloc(sizeof(char)*(str1.length + str2.length + 1));
	if (str3.ch == NULL)
		return error;
	else
	{
		for (i = 0; i < str1.length; i++)//不要把最后的"\0"复制过去
			str3.ch[i] = str1.ch[i];
		for (j = 0; j <= str2.length;j++)//要把最后的"\0"复制过去
			str3.ch[j+i] = str2.ch[j];
		str3.length = str1.length + str2.length;
		return ok;
	}
}
int substring(str str1, int pos,int len , str &substr)//求str1的从位置pos开始长度为len的子串substr
{
	if (pos < 1 || len < 0 || len + pos - 1>str1.length || pos>str1.length)
		return error;
	if (substr.ch != NULL)
	{
		free(substr.ch);
		substr.ch = NULL;
		substr.length = 0;
	}
	substr.ch = (char*)malloc(sizeof(char)*(len + 1));
	if (substr.ch == NULL)
		return error;
	else
	{
		for (int i = pos - 1; i < pos + len - 1; i++)
			substr.ch[i-pos + 1] = str1.ch[i];
		substr.ch[len] = NULL;
		substr.length = len;
		return ok;
	}
			
}
int match(str str1, int &pos, str substr)//查找substr在str1中的位置
{
	int i = 0, j = 0, k = 0;
	if (substr.length > str1.length)
	{
		pos = 0;
		return error;
	}
	while (i < str1.length && j < substr.length)
		if (str1.ch[i] != substr.ch[j])
		{
			i = ++k;
			j = 0;
		}
		else
		{
			i++; j++;
		}
	if (j == substr.length)
	{
		pos = i - j + 1;
		return ok;
	}
	else
		return n
}
void main() //演示串的各种操作
{
	int pos = 3, len = 4, pos1;
	char a[] = "i love you";
	char b[] = " forever!";
	str string1, string2, string3;
	init(string1); init(string2); init(string3);
	if (strcopy2(b, string1))
		if (strcopy1(string1, string2))
			if (strcopy2(a, string1))
			{
				if (strcontact(string1, string2, string3))
					cout <<"“"<<string1.ch << "”"<< "和" << "“" << string2.ch << "”" << "连接起来是" << "“" << string3.ch << "”" << endl;
				else
					cout << "连接失败" << endl;
				if (substring(string1, pos, len, string3))
				{
					cout << "“" << string1.ch << "”" << "从位置"<<pos<<"开始长度为"<<len<<"的子串为" << "“" << string3.ch << "”" << endl;
					if (match(string1, pos1, string3))
						cout <<"匹配成功! " << "“" << string3.ch << "”" << "在" << "“" << string1.ch << "”" << "中的位置是:" << pos1 << endl;
					else
						cout << "串匹配失败" << endl;
					int c = strcompare(string1, string3);
					if (c == 0)
						cout << "“" << string1.ch << "”" << "=" << "“" << string3.ch << "”" << endl;
						else if (c<0)
							cout << "“" << string1.ch << "”" << "<" << "“" << string3.ch << "”" << endl;
						else
							cout << "“" << string1.ch << "”" << ">" << "“" << string3.ch << "”" << endl;
				}
				else
					cout << "取子串失败" << endl;
			}
			else
				cout << "数组复制到串失败" << endl;
		else
			cout << "串复制到串失败" << endl;
	else
		cout << "数组制到串失败" << endl;		
}

3、运行结果

1


  版权声明:本文参考了严蔚敏的《 数据结构》。未经作者允许,严禁用于商业出版,否则追究法律责任。网络转载请注明出处,这是对原创者的起码的尊重!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝月心语

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值