串的基本操作实现源码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
*tmp++ = *stmp++;
}
for ( i = 0; i != T.length; ++i)
{
++stmp;
}
for ( i = 0; i != V.length; ++i)
{
*tmp++ = *vtmp++;
}
while(*stmp)
{
*tmp++ = *stmp++;
}
*tmp = '\0';
{
++Str.ch;
}
for ( 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)
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)
delete Str.ch;
Str.length = nlength;
Str.ch = ctemp;
pos = Index(Str, T, pos + V.length);
}
printf("Replace()函数执行,子串替代成功\n");
return true;
}
//11.StrInsert(&S, pos, T)
{
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;
exit(0);
}
Str.ch = new char[1];
*Str.ch = '\0';
Str.length = 0;
printf("ClearString()函数执行,串已清空\n");
return true;
}
//7.Concat(&T, S1, S2)
bool Concat(HString &T, HString S1, HString S2)
{
if (*Ttmp++ != *Stmp++)
{
flag = false;
break;
}
}
if (flag)
{
printf("Index()函数执行,主串S第%d个字符之后第一次出现与串T相同的字串的位置为:%d\n", pos, i);
return i;
}
}
Str.ch++;
}
printf("Index()函数执行,主串第%d个字符之后未找到与串T相同的字串\n", pos);
*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( i = 0; i != len; ++i)
}
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';
printf("StrCopy函数执行,串拷贝成功\n");
return true;
}
//3.StrEmpty(S)
bool StrEmpty(HString Str)
{
if (!Str.ch)
{
printf("StrEmpty()函数执行,串不存在,程序即将退出\n");
{
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)
{
*tmp++ = *Str.ch++;
}
*tmp = '\0';
printf("SubString()函数执行,字串获取成功\n");
return true;
}
//9.Index(S, T, pos)
int Index(HString Str, HString T, int pos)
{
if (!Str.ch||!T.ch||(0 == T.length))
system("pause");
exit(0);
}
else
{
if (!Str.length)
{
printf("StrEmpty()函数执行,串为空\n");
return true;
}
else
{
printf("StrEmpty()函数执行,串非空\n");
return false;
}
}
}
//4.StrCompare(S, T)
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];
#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)
*tmp = '\0';
delete[] Str.ch;
Str.ch = ctemp;
Str.length = nlength;
printf("StrInsert()函数执行,在主串第%d个字符前插入字串成功\n", pos);
return true;
}
//12.StrDelete(&S, pos, len)
return 0;
}
//10.Replace(&S, T, V)
bool Replace(HString &Str, HString T, HString V)
{
if ((!Str.ch)||(!T.ch)||(!V.ch)||(0 == T.length))
{
printf("Replace()函数执行,串不存在或为空,程序即将退出\n");
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++;
bool SubString(HString &Sub, HString Str, int pos, int len)
{
if (Sub.ch)
{
delete[] Sub.ch;
}
if (!Str.ch)
{
printf("SubString()函数执行,串为空,程序即将退出\n");
system("pause");
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);
{
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);
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, S, pos, len)
}
int nlength = Str.length + T.length;
char *ctemp = new char[nlength + 1];
if (!ctemp)
{
printf("StrInsert()函数执行,内存分配失败,程序即将退出\n");
stem("pause");
exit(-1);
{
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)
bool StrInsert(HString &Str, int pos, HString T)
{
if ((pos < 1)||(pos > Str.length + 1)||(NULL == T.ch))
{
printf("StrInsert()函数执行,pos参数错误或串不存在\n");
return false;
bool StrDelete(HString &Str, int pos, int len)
{
if ((pos < 1)||(pos > Str.length - len + 1))
{
printf("StrDelete()函数执行,输入参数错误\n");
return Str.length;
}
//6..ClearString(&S)
bool ClearString(HString &Str)
{
if (Str.ch)
{
delete[] Str.ch;
}
else
{
printf("ClearString()函数执行,串不存在,程序即将退出\n");
system("pause");
{
if (T.ch)
{
delete[] T.ch;
}
T.length = S1.length + S2.length;
T.ch = new char[T.length + 1];
相关文档
最新文档