# 顺序存储的串结构的基本原理

串是由零个或多个字符组成的有限序列,
当串中只有零个字符时称为空串,串的长度为字符的个数,
由全部字符组成的串称为主串,
在由n个字符组成的主串中,由k(k != n)个连续字符组成的串称为子串。
此处,通过顺序表来构成了一个有限空间的字符串。

# 顺序存储的串结构的基本创建

SeqString_.h(头文件)
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define MAX_SPACE 21
#define DEBUG
typedef unsigned char u_char;
void Init_SeqString(u_char (*SeqString)[MAX_SPACE]);
void Show_Menu();
int Strlen(const char* str);
void StrAssign(u_char (*SeqString)[MAX_SPACE],const char* str);
void Show_SeqString(const u_char (*SeqString)[MAX_SPACE]);
void StrCopy(u_char* SeqString, const char* str);
int StrCompare(const u_char* SeqString,const char* str);
void StrConcat(u_char* SeqString, const char* str);
int SubString(const u_char* SeqString,char* S_str, const int index, const int s_length);
void StrInsert(u_char* SeqString, const char* str, const int index);
int StrIndex(const u_char* SeqString, const char* str);
void StrDelete(u_char(*SeqString)[MAX_SPACE], const char* str);
void StrReplace(u_char* SeqString, const char* str,const char* r_str);
void StrClean(u_char* SeqString);
void StrDestroy(u_char* SeqString);
SeqString_functions.c
#include"SeqString_.h"
void Init_SeqString(u_char(*SeqString)[MAX_SPACE])
{
	assert(NULL != SeqString);
	(*SeqString)[0] = '\0';
}
void Show_Menu()
{
	printf("\n********************************************\n");
	printf(" [1]  StrAssign     *********[2]  Strlen   *\n");
	printf(" [3]  Show_SeqString*********[4]  StrCopy  *\n");
	printf(" [5]  StrCompare    *********[6]  StrConcat*\n");
	printf(" [7]  SubString     *********[8]  StrInsert*\n");
	printf(" [9]  StrIndex      *********[10] StrDelete*\n");
	printf(" [11] StrReplace    *********[12] StrClean *\n");
	printf(" [13] StrDestroy                           *\n");
	printf(" [0]  SystemExit                           *\n");
	printf("********************************************\n");
}
int Strlen(const char* str)
{
	assert(NULL != str);
	int counting = 0;
	while (*str)
	{
		++counting;
		++str;
	}
#ifdef DEBUG
	printf("字符串长度为%d\n", counting);
#endif
	return counting;
}
void StrAssign(u_char (*SeqString)[MAX_SPACE],const char* str)
{
	assert(NULL != SeqString && NULL != str);
	int length = Strlen(str);
	if (length >= MAX_SPACE - 1)
	{
		int i = 0;
		for (i = 0; i < MAX_SPACE - 1; ++i)
			(*SeqString)[i] = *str++;
		(*SeqString)[MAX_SPACE - 1] = '\0';
	}
	else
	{
		int i = 0;
		for (i = 0; i < length; ++i)
			(*SeqString)[i] = *str++;
		(*SeqString)[length] = '\0';
	}
}
void Show_SeqString(const u_char (*SeqString)[MAX_SPACE])
{
	assert(NULL != SeqString);
	printf("%s\n", SeqString);
}
void StrCopy(u_char* SeqString,const char* str)
{
	assert(NULL != SeqString && NULL != str);
	int length = Strlen(str);
	if (length >= MAX_SPACE - 1)
	{
		int i = 0;
		for (i = 0; i < MAX_SPACE - 1; ++i)
			*SeqString++ = *str++;
	}
	else
	{
		while (*str)
			*SeqString++ = *str++;
	}
	*SeqString = '\0';
}
int StrCompare(const u_char* SeqString, const char* str)
{
	assert(NULL != SeqString && NULL != str);
	while (*SeqString)
	{
		if (*SeqString > *str)
		{
			#ifdef DEBUG
				printf("大于输入的字符串\n");
			#endif
			return 1;
		}
		else if (*SeqString < *str)
		{
			#ifdef DEBUG
				printf("小于输入的字符串\n");
			#endif
			return -1;
		}
		++str;
		++SeqString;
	}
	#ifdef DEBUG
		printf("等于输入的字符串\n");
	#endif
	return 0;
}
void StrConcat(u_char* SeqString, const char* str)
{
	int length_SS = Strlen(SeqString);
	if (MAX_SPACE - 1 == length_SS)
		return;
	else
	{
		while (*SeqString)
			++SeqString;
		while (*str)
		{
			if (MAX_SPACE - 1 == length_SS)
				break;
			*SeqString++ = *str++;
			++length_SS;
		}
		*SeqString = '\0';
	}
}
int SubString(const u_char* SeqString, char* S_str, const int index, const int s_length)
{
	assert(NULL != SeqString);
	int length = Strlen(SeqString);
	if (!(SeqString))
		return 0;
	else if (index <= 0 || index >= length)
		return 0;
	else if (index + s_length > MAX_SPACE - 1)
		return 0;
	else
	{
		int i = 0;
		while (++i < index)
			++SeqString;
		for (i = 0; i < s_length; ++i)
			*S_str++ = *SeqString++;
		*S_str = '\0';
		return 1;
	}
}
void StrInsert(u_char* SeqString, const char* str, const int index)
{
	assert(NULL != SeqString && NULL != str);
	int length_SS = Strlen(SeqString);
	int length_str = Strlen(str);
	if (!(length_SS) || !(length_str))
		return;
	else if (index <= 0 || index > length_SS)
		return;
	else if (length_str + length_SS > MAX_SPACE - 1)
		return;
	else
	{
		int i = 0;
		int M_Time = length_str;
		char* P_str = SeqString + index - 1;
		while (M_Time)
		{
			char* P_T = SeqString + length_SS;
			char* P_N_T = P_T - 1;
			for (i = 0; i < length_SS - index + 1; ++i)
				*P_T-- = *P_N_T--;
			++length_SS;
			--M_Time;
		}
		while (*str)
			*P_str++ = *str++;
		*(SeqString + length_SS) = '\0';
	}
}
int StrIndex(const u_char* SeqString, const char* str)
{
	assert(NULL != SeqString && NULL != str);
	int length_S = Strlen(SeqString);
	
	if (!(length_S))
		return -1;
	else
	{
		int i = length_S;
		int counting = 0;
		while (i)
		{
			const char* P_str = str;
			++counting;
			if (*str == *SeqString)
			{
				const u_char* P_SS = SeqString;
				int flag = 1;
				while (*P_str)
				{
					if (*P_str++ != *P_SS++)
					{
						flag = 0;
						break;
					}
				}
				if (flag)
					return counting;
			}
			++SeqString;
			--i;
		}
	}
	return -1;
}
void StrDelete(u_char (*SeqString)[MAX_SPACE], const char* str)
{
	assert(NULL != SeqString && NULL != str);
	int length_SS = Strlen(SeqString[0]);
	if (!(length_SS))
		return;
	else
	{
		int index = 0;
		while (index = StrIndex(SeqString[0], str), -1 != index)
		{
			int i = 0;
			int length_str = Strlen(str);
			while (length_str)
			{
				for (i = index - 1; i < length_SS - 1; ++i)
					(*SeqString)[i] = (*SeqString)[i + 1];
				
				if (1 == length_SS)
					(*SeqString)[0] = '\0';
				else
					(*SeqString)[i] = '\0';
				--length_str;
				--length_SS;
			}
		}
	}
}
// 当用类似于 12 替换成 121,会有个重复调用的 bug,有空再换个写法。
void StrReplace(u_char* SeqString, const char* str, const char* r_str)
{
	assert(NULL != SeqString && NULL != str && NULL != r_str);
	int length_SS = Strlen(SeqString);
	if (!(length_SS))
		return;
	else
	{
		int index = 0;
		int length_str = Strlen(str);
		int length_rs = Strlen(r_str);
		if (length_str > length_rs)
		{
			while (index = StrIndex(SeqString, str), -1 != index)
			{
				char* P_N_str = NULL;
				char* P_str = SeqString + index - 1;
				while (*r_str)
					*P_str++ = *r_str++;
				P_N_str = P_str + 1;
				while (*P_N_str)
					*P_str++ = *P_N_str++;
				*P_str = '\0';
			}
		}
		else if (length_str < length_rs)
		{
			if (length_SS + length_rs > MAX_SPACE - 1)
				return;
			else
			{
				while (index = StrIndex(SeqString, str), -1 != index)
				{
					int M_time = length_rs - length_str;
					int M_element = length_SS - length_str - index + 1;
					char* P_str = SeqString + index - 1;
					while (M_time)
					{
						int i = 0;
						char* P_T = SeqString + length_SS;
						char* P_H_T = SeqString + length_SS - 1;
						for (i = 0; i < M_element; ++i)
							*P_T-- = *P_H_T--;
						++length_SS;
						--M_time;
					}
					while (*r_str)
					{
						*P_str++ = *r_str++;
					}
					*(SeqString + length_SS) = '\0';
				}
			}
		}
		else
		{
			while (index = StrIndex(SeqString, str), -1 != index)
			{
				char* P_str = SeqString + index - 1;
				while (*r_str)
					*P_str++ = *r_str++;
			}
		}
	}
}
void StrClean(u_char* SeqString)
{
	assert(NULL != SeqString);
	*SeqString = '\0';
}
void StrDestroy(u_char* SeqString)
{
	assert(NULL != SeqString);
	StrClean(SeqString);
}
SeqString_main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqString_.h"
int main()
{
	char str[MAX_SPACE];
	char r_str[MAX_SPACE];
	char S_str[MAX_SPACE];
	u_char SeqString[MAX_SPACE];
	int option = 0;
	int index = 0;
	int s_length = 0;
	Init_SeqString(&SeqString);
	Show_Menu();
	while (printf("选择是>"), scanf("%d", &option), printf("\n"), option)
	{
		getchar();
		switch (option)
		{
		case 1:
			printf("输入的字符串>");
			scanf("%s", str);
			StrAssign(&SeqString, str);
			break;
		case 2:
			Strlen(SeqString);
			break;
		case 3:
			Show_SeqString(&SeqString);
			break;
		case 4:
			printf("拷贝的字符串>");
			scanf("%s", str);
			StrCopy(SeqString, str);
			break;
		case 5:
			printf("需比较的字符串>");
			scanf("%s",str);
			StrCompare(SeqString, str);
			break;
		case 6:
			printf("需接入的字符串>");
			scanf("%s", str);
			StrConcat(SeqString,str);
			break;
		case 7:
			printf("字符字串起始位置与长度>");
			scanf("%d %d", &index, &s_length);
			if(SubString(SeqString, S_str, index, s_length))
				printf("%s", S_str);
			break;
		case 8:
			printf("插入字符与位置>");
			scanf("%s %d", str, &index);
			StrInsert(SeqString,str,index);
			break;
		case 9:
			printf("寻找的字符或字符串>");
			scanf("%s", str);
			printf("字符或字符串起始位置为%d", StrIndex(SeqString, str));
			break;
		case 10:
			printf("需删除的字符或字符串>");
			scanf("%s", str);
			StrDelete(&SeqString, str);
			break;
		case 11:
			printf("被替换的字符与替换字符>");
			scanf("%s %s", str, r_str);
			StrReplace(SeqString, str, r_str);
			break;
		case 12:
			StrClean(SeqString);
			break;
		case 13:
			StrDestroy(SeqString);
			break;
		default: printf("不在选择范围内\n");
		}
		Show_Menu();
	}
	StrDestroy(SeqString);
	printf("已退出\n");
	return 0;
}