#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *ch;
int length;
}HString;
bool StrAssign(HString &T, char *chars)
{
T.length = 0;//对应79
if (T.ch)
delete T.ch; //清空 T
char *ctemp = chars;//指向 chars指向的 内存地址
while(*ctemp)
{ //取chars的长度
++T.length;
++ctemp;//ctemp指针后移
}
T.ch = new char[T.length + 1];// 分配空间 长度为所需长度+1 (+1原因看29)
char *tmp = T.ch;//指向 新开空间的 内存地址
while(*chars)
*tmp++ = *chars++;//赋值后 指针后移
*tmp = '\0';//补 呼应 23
return true;
}
bool StrCopy(HString &T, HString Str)
{
if (!Str.ch)
return false;//被复制串不存在直接退
T.length = Str.length;
if (T.ch)
delete T.ch;//清空T
T.ch = new char[T.length + 1];// 分配空间 长度为所需长度+1 (+1原因看49)
char *tmp = T.ch;//指向 新开空间的 内存地址
while(*Str.ch)
*tmp++ = *Str.ch++;//赋值后 指针后移
*tmp = '\0';//补 呼应 43
return true;
}
bool StrEmpty(HString Str)
{
if (!Str.ch)
return false;//判定的串不存在直接退
else
{
if (!Str.length)
return true;//空 长度为0
else
return false;//非空 长度为N
}
}
int StrCompare(HString Str, HString T)
{
if ((!T.ch)||(!Str.ch))
return 0;//比较的两个串有一个不存在就退
int flag = 0;
int len1=Str.length,len2=T.length;
for (int i = 0; (i < len1)&&(i < len2); i++)
{
if (*(T.ch + i) != *(Str.ch + i))
{//如果出现不相等的字符就记录两个字符的ASCII码差值 直接跳出就行了
flag = *(Str.ch + i) - *(T.ch + i);
break;
}
}
if (0 == flag)//如果73行的循环没有改变flag的值 证明一个包含另一个
flag = len1 - len2;
return flag;
}
int Strlength(HString Str)
{
if (!Str.ch)
return false;//判定的串不存在直接退
return Str.length;
}
bool SubString(HString &Sub, HString Str, int pos, int len)
{
if (!Str.ch)
return false;//判定的串不存在直接退
if (Sub.ch)
delete Sub.ch;//清空Sub
if ((pos < 1)||(pos > Str.length)||(len < 0)||(len > Str.length - pos +1))
{//相当于撤销清空操作
Sub.ch = new char[1];
*Sub.ch = '\0';
Sub.length = 0;
return false;
}
Sub.ch = new char[len + 1];// 分配空间 长度为所需长度+1 (+1原因看122)
char *tmp = Sub.ch;//指向 Sub的ch指针所指的 内存地址
for(int i = 1; i != pos; i++)
{
Str.ch++;//找到要复制字串的头(移动的是指针)
}
for(int i = 0; i != len; i++)
{//从pos开始的len长的字串
*tmp++ = *Str.ch++;
}
*tmp = '\0';//补 呼应 109
return true;
}
bool Concat(HString &T, HString S1, HString S2)
{
if (T.ch)
delete T.ch;//清空T
T.length = S1.length + S2.length;
T.ch = new char[T.length + 1];// 分配空间 长度为所需长度+1 (+1原因看145)
char *tmp = T.ch;//指向 新开空间的 内存地址
while(*S1.ch)
{//先复制S1
*tmp++ = *S1.ch++;
}
while(*S2.ch)
{//此时的*tmp指针已经指向S2的 预复制内存地址
*tmp++ = *S2.ch++;
}
*tmp = '\0';//补 呼应 145
return true;
}
int Index(HString Str, HString T)
{
if (!Str.ch||!T.ch||(0 == T.length))
return 0;//若两个串不存在或操作不合逻辑直接退
for (int i = 1; i <= Str.length - T.length + 1; i++)
{
if (*Str.ch == *T.ch)
{//判断字串第一个字符来确定是否进入判断循环
bool flag = true;
char *Stmp = Str.ch,*Ttmp = T.ch;//准备好两个比较指针
while(*Ttmp)
{
if (*Ttmp++ != *Stmp++)
{
flag = false;
break;
}
}
if (flag)//如果标记不变 返回数值
return i;
}
Str.ch++;//不同字符Str的指针要移动
//如果相同但匹配失败 Str的指针要移动 但T的指针不需要移动
}
return 0;
}
bool ClearString(HString &Str)
{
if (Str.ch)
delete Str.ch;//清空Str
Str.ch = new char[1];//分配空间
*Str.ch = '\0';//空串
Str.length = 0;
return true;
}
bool DestoryString(HString &Str)
{
if (Str.ch)
delete Str.ch;//清空Str
Str.ch = NULL;//死串
Str.length = 0;
return true;
}
bool InitString(HString &T)
{//字符串初始化
T.ch = NULL;
T.length = 0;
return true;
}
bool PrintString(HString T)
{//字符串打印
if (!T.ch)
return false;
else
{
printf("串打印:");
while(*T.ch)
printf("%c", *T.ch++);
printf("\n");
return true;
}
return true;
}
int main()
{
HString T, T1, T2 ,T3;
InitString(T);
InitString(T1);
InitString(T2);
InitString(T3);
char *test = (char*)"abcdefg";
char *test1 = (char*)"h i j k l m n ";
char *test2 = (char*)"/o/p/q/r/s/t";
char *test3 = (char*)"uvwxyz";
StrAssign(T, test);
PrintString(T);
StrAssign(T1, test1);
PrintString(T1);
StrAssign(T2, test2);
PrintString(T2);
StrAssign(T3, test3);
PrintString(T3);
DestoryString(T);
DestoryString(T1);
DestoryString(T2);
DestoryString(T3);
return 0;
}
05-12
9007
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-14
131
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-14
102
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-16
873
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-17
713
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交