前端之家收集整理的这篇文章主要介绍了
【数据结构】 串的基本操作,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- /*
- ==========================================================================================
- 串的基本操作
- By~fanxingzju 2014.04.23
- 1.StrAssign(&T,chars)
- 初始条件:chars是字符串常量
- 操作结果:生成一个其值等于chars的串T
- 2.StrCopy(&T,S)
- 初始条件:串S存在
- 操作结果:由串S复制的串T
- 3.StrEmpty(S)
- 初始条件:串S存在
- 操作结果:若S为空串,则返回true,否则返回false
- 4.StrCompare(S,T)
- 初始条件:串S和T存在
- 操作结果:若S > T,则返回值 > 0;若S = T,则返回值 = 0;若S < T,则返回值 < 0
- 5.StrLength(S)
- 初始条件:串S存在
- 操作结果:返回S的元素个数,称为串的长度
- 6.ClearString(&S)
- 初始条件:串S存在
- 操作结果:将S清为空串
- 7.Concat(&T,S1,S2)
- 初始条件:串S1和S2存在
- 操作结果:用T返回由S1和S2联接而成的新串
- 8.SubString(&Sub,S,pos,len)
- 初始条件:串S存在,1≦pos≦Strlength(S)且0≦len≦Strlength(S)-pos+1
- 操作结果:用Sub返回串S的第pos个字符起长度为len的字串
- 9.Index(S,T,pos)
- 初始条件:串S和T存在,T为非空串,1≦pos≦Strlength(S)
- 操作结果:若主串中存在和串T值相同的字串,则返回它在字串S中第pos个字符之后第一次出现的位置;否则函数值为0
- 10.Replace(&S,V)
- 初始条件:串S,T和V存在,T是非空串
- 操作结果:用V替换主串S中出现的所有与T相等的不重叠的字串
- 11.StrInsert(&S,T)
- 初始条件:串S和T存在,1≦pos≦Strlength(S)+1
- 操作结果:在串S的第pos个位置字符之前传入串T
- 12.StrDelete(&S,len)
- 初始条件:串S存在,1≦pos≦Strlength(S)-len+1
- 操作结果:从串S中删除第pos个字符起长度为len的字串
- 13.DestroyString(&S)
- 初始条件:串S存在
- 操作结果:串S被销毁
- 14.PrintString(T)
- 初始条件:串S存在
- 操作结果:将S打印在屏幕上
- 15.InitString(&T)
- 初始条件:串T已经定义
- 操作结果:将串S初始化
- ==========================================================================================
- 备注:
- 1.这里将空串定义为:T.ch = new char[1]; T.length = 0; *T.ch = '\0';
- 2.这里将不存在的串视为:T.ch = NULL;
- 3.字符串全部以'\0'结束
- 4.尽量保持各个函数的独立性,只有在Replace()函数中调用了Index()函数
- 5.函数中的打印信息可以根据需要选择
- 6.由于函数比较多,此处不再提供详细的测试代码,可以根据需要自行设计测试特定函数
- */
-
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef struct
- {
- char *ch;
- int length;
- }HString;
-
- //1.StrAssign(&T,chars)
- bool StrAssign(HString &T,char *chars)
- {
- if (T.ch)
- {
- delete[] T.ch;
- }
-
- char *ctemp = chars;
- T.length = 0;
-
- while(*ctemp)
- {
- ++T.length;
- ++ctemp;
- }
-
- T.ch = new char[T.length + 1];
- if (!T.ch)
- {
- printf("StrAssign()函数执行,内存分配失败,程序即将退出\n");
- system("pause");
- exit(-1);
- }
-
- char *tmp = T.ch;
- while(*chars)
- {
- *tmp++ = *chars++;
- }
- *tmp = '\0';
-
- printf("StrAssign()函数执行,串T生成成功\n");
- return true;
- }
-
- //2.StrCopy(&T,S)
- bool StrCopy(HString &T,HString Str)
- {
- if (!Str.ch)
- {
- printf("StrCopy()函数执行,被拷贝串不存在,程序即将退出\n");
- system("pause");
- exit(0);
- }
- if (T.ch)
- {
- delete[] T.ch;
- }
- T.length = Str.length;
-
- T.ch = new char[T.length + 1];
- if (!T.ch)
- {
- printf("StrCopy()函数执行,内存分配失败,程序即将退出\n");
- system("pause");
- exit(-1);
- }
- char *tmp = T.ch;
- while(*Str.ch)
- {
- *tmp++ = *Str.ch++;
- }
- *tmp = '\0';
-
- printf("StrCopy函数执行,串拷贝成功\n");
- return true;
- }
-
- //3.StrEmpty(S)
- bool StrEmpty(HString Str)
- {
- if (!Str.ch)
- {
- printf("StrEmpty()函数执行,串不存在,程序即将退出\n");
- system("pause");
- exit(0);
- }
- else
- {
- if (!Str.length)
- {
- printf("StrEmpty()函数执行,串为空\n");
- return true;
- }
- else
- {
- printf("StrEmpty()函数执行,串非空\n");
- return false;
- }
- }
- }
-
- //4.StrCompare(S,T)
- int StrCompare(HString Str,HString T)
- {
- if ((!T.ch)||(!Str.ch))
- {
- printf("StrCompare()函数执行,程序即将退出\n");
- system("pause");
- exit(0);
- }
- int flag = 0;
- for (int i = 0; (i < Str.length)&&(i < T.length); ++i)
- {
- if (*(T.ch + i) != *(Str.ch + i))
- {
- flag = *(Str.ch + i) - *(T.ch + i);
- break;
- }
- }
- if (0 == flag)
- {
- flag = Str.length - T.length;
- }
- printf("StrCompare()函数执行,比较的返回值为: %d \n",flag);
- return flag;
- }
-
- //5.StrLength(S)
- int Strlength(HString Str)
- {
- if (!Str.ch)
- {
- printf("Strlength()函数执行,串不存在,程序即将退出\n");
- system("pause");
- exit(0);
- }
- printf("Strlength()函数执行,返回的串的长度为: %d \n",Str.length);
- return Str.length;
- }
-
- //6.ClearString(&S)
- bool ClearString(HString &Str)
- {
- if (Str.ch)
- {
- delete[] Str.ch;
- }
- else
- {
- printf("ClearString()函数执行,串不存在,程序即将退出\n");
- system("pause");
- exit(0);
- }
- Str.ch = new char[1];
- *Str.ch = '\0';
- Str.length = 0;
- printf("ClearString()函数执行,串已清空\n");
- return true;
- }
-
- //7.Concat(&T,S2)
- bool Concat(HString &T,HString S1,HString S2)
- {
- if (T.ch)
- {
- delete[] T.ch;
- }
- T.length = S1.length + S2.length;
- T.ch = new char[T.length + 1];
- if (!T.ch)
- {
- printf("Concat()函数执行,内存分配失败,程序即将退出\n");
- system("pause");
- exit(-1);
- }
- char *tmp = T.ch;
- while(*S1.ch)
- {
- *tmp++ = *S1.ch++;
- }
- while(*S2.ch)
- {
- *tmp++ = *S2.ch++;
- }
- *tmp = '\0';
- printf("Concat()函数执行,串链接成功\n");
- return true;
- }
-
- //8.SubString(&Sub,len)
- bool SubString(HString &Sub,HString Str,int pos,int len)
- {
- if (Sub.ch)
- {
- delete[] Sub.ch;
- }
- if (!Str.ch)
- {
- printf("SubString()函数执行,串为空,程序即将退出\n");
- system("pause");
- exit(0);
- }
- if ((pos < 1)||(pos > Str.length)||(len < 0)||(len > Str.length - pos +1))
- {
- printf("SubString()函数执行,参数pos和len有错误,获取字串失败\n");
- Sub.ch = new char[1];
- *Sub.ch = '\0';
- Sub.length = 0;
- return false;
- }
- Sub.ch = new char[len + 1];
- char *tmp = Sub.ch;
- for(int i = 1; i != pos; ++i)
- {
- Str.ch++;
- }
- for(int i = 0; i != len; ++i)
- {
- *tmp++ = *Str.ch++;
- }
- *tmp = '\0';
- printf("SubString()函数执行,字串获取成功\n");
- return true;
- }
-
- //9.Index(S,pos)
- int Index(HString Str,HString T,int pos)
- {
- if (!Str.ch||!T.ch||(0 == T.length))
- {
- printf("Index()函数执行,串不存在或为空串,程序即将退出\n");
- system("pause");
- exit(0);
- }
- if ((pos < 1)||(pos > Str.length))
- {
- printf("Index()函数执行,pos参数错误\n");
- return 0;
- }
- for(int i = 1; i != pos; ++i)
- {
- ++Str.ch;
- }
- for (int i = pos; i != Str.length - T.length + 2; ++i)
- {
- if (*Str.ch == *T.ch)
- {
- bool flag = true;
- char *Stmp = Str.ch;
- char *Ttmp = T.ch;
- while(*Ttmp)
- {
- if (*Ttmp++ != *Stmp++)
- {
- flag = false;
- break;
- }
- }
- if (flag)
- {
- printf("Index()函数执行,主串S第%d个字符之后第一次出现与串T相同的字串的位置为:%d\n",i);
- return i;
- }
- }
- Str.ch++;
- }
- printf("Index()函数执行,主串第%d个字符之后未找到与串T相同的字串\n",pos);
- return 0;
- }
-
- //10.Replace(&S,V)
- bool Replace(HString &Str,HString V)
- {
- if ((!Str.ch)||(!T.ch)||(!V.ch)||(0 == T.length))
- {
- printf("Replace()函数执行,串不存在或为空,程序即将退出\n");
- system("pause");
- exit(0);
- }
- int pos = Index(Str,1);
- while(pos)
- {
- int nlength = Str.length - T.length + V.length;
- char *ctemp = new char[nlength + 1];
- if (!ctemp)
- {
- printf("Replace()函数执行,内存分配失败,程序即将退出\n");
- system("pause");
- exit(-1);
- }
- char *tmp = ctemp;
- char *stmp = Str.ch;
- char *vtmp = V.ch;
- for (int i = 1; i != pos; ++i)
- {
- *tmp++ = *stmp++;
- }
- for (int i = 0; i != T.length; ++i)
- {
- ++stmp;
- }
- for (int i = 0; i != V.length; ++i)
- {
- *tmp++ = *vtmp++;
- }
- while(*stmp)
- {
- *tmp++ = *stmp++;
- }
- *tmp = '\0';
- delete Str.ch;
- Str.length = nlength;
- Str.ch = ctemp;
- pos = Index(Str,pos + V.length);
- }
- printf("Replace()函数执行,子串替代成功\n");
- return true;
- }
-
- //11.StrInsert(&S,T)
- bool StrInsert(HString &Str,HString T)
- {
- if ((pos < 1)||(pos > Str.length + 1)||(NULL == T.ch))
- {
- printf("StrInsert()函数执行,pos参数错误或串不存在\n");
- return false;
- }
- int nlength = Str.length + T.length;
- char *ctemp = new char[nlength + 1];
- if (!ctemp)
- {
- printf("StrInsert()函数执行,内存分配失败,程序即将退出\n");
- system("pause");
- exit(-1);
- }
- char *tmp = ctemp;
- char *stmp = Str.ch;
- for (int i = 1; i != pos; ++i)
- {
- *tmp++ = *stmp++;
- }
- while(*T.ch)
- {
- *tmp++ = *T.ch++;
- }
- while(*stmp)
- {
- *tmp++ = *stmp++;
- }
- *tmp = '\0';
- delete[] Str.ch;
- Str.ch = ctemp;
- Str.length = nlength;
- printf("StrInsert()函数执行,在主串第 %d 个字符前插入字串成功\n",pos);
- return true;
- }
-
- //12.StrDelete(&S,len)
- bool StrDelete(HString &Str,int len)
- {
- if ((pos < 1)||(pos > Str.length - len + 1))
- {
- printf("StrDelete()函数执行,输入参数错误\n");
- return false;
- }
- int nlength = Str.length - len;
- char *ctemp = new char[nlength + 1];
- if (!ctemp)
- {
- printf("StrDelete()函数执行,内存分配失败,程序即将退出\n");
- system("pause");
- exit(-1);
- }
- char *tmp = ctemp;
- char *stmp = Str.ch;
- for (int i = 1; i != pos; ++i)
- {
- *tmp++ = *stmp++;
- }
- for (int i = 0; i != len; ++i)
- {
- ++stmp;
- }
- while(*stmp)
- {
- *tmp++ = *stmp++;
- }
- *tmp = '\0';
- delete[] Str.ch;
- Str.ch = ctemp;
- Str.length = nlength;
- printf("StrDelete()函数执行,指定位置 %d 和长度 %d 的子串删除成功\n",len);
- return true;
- }
- //13.DestroyString(&S)
- bool DestoryString(HString &Str)
- {
- if (Str.ch)
- {
- delete[] Str.ch;
- }
- Str.ch = NULL;
- Str.length = 0;
- printf("DestoryString()函数执行,串销毁成功\n");
- return true;
- }
-
- //14.PrintString(T)
- bool PrintString(HString T)
- {
- if (!T.ch)
- {
- printf("PrintString()函数执行,串不存在\n");
- return false;
- }
- else
- {
- printf("PrintString()函数执行,串的长度为 %d ,打印结果如下:",T.length);
- while(*T.ch)
- {
- printf("%c",*T.ch++);
- }
- printf("\n");
- return true;
- }
- }
-
- //15.InitString(&T)
- bool InitString(HString &T)
- {
- T.ch = NULL;
- T.length = 0;
- return true;
- }
-
- int main()
- {
- HString T,T1,T2;
- InitString(T);
- InitString(T1);
- InitString(T2);
-
- char *test = "abc abc abc abc a";
- char *test1 = "a";
- char *test2 = "/Here/";
-
- StrAssign(T,test);
- PrintString(T);
-
- StrAssign(T1,test1);
- PrintString(T1);
-
- StrAssign(T2,test2);
- PrintString(T2);
-
- //TODO:
-
- DestoryString(T);
- DestoryString(T1);
- DestoryString(T2);
-
- system("pause");
- return 0;
- }