#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *ch;
int length;
}HString;
bool StrAssign(HString &T, char *chars)
{
T.length = 0;
if (T.ch)
delete T.ch;
char *ctemp = chars;
while(*ctemp)
{
++T.length;
++ctemp;
}
T.ch = new char[T.length + 1];
char *tmp = T.ch;
while(*chars)
*tmp++ = *chars++;
*tmp = '\0';
return true;
}
bool StrCopy(HString &T, HString Str)
{
if (!Str.ch)
return false;
T.length = Str.length;
if (T.ch)
delete T.ch;
T.ch = new char[T.length + 1];
char *tmp = T.ch;
while(*Str.ch)
*tmp++ = *Str.ch++;
*tmp = '\0';
return true;
}
bool StrEmpty(HString Str)
{
if (!Str.ch)
return false;
else
{
if (!Str.length)
return true;
else
return false;
}
}
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))
{
flag = *(Str.ch + i) - *(T.ch + i);
break;
}
}
if (0 == 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;
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];
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';
return true;
}
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];
char *tmp = T.ch;
while(*S1.ch)
{
*tmp++ = *S1.ch++;
}
while(*S2.ch)
{
*tmp++ = *S2.ch++;
}
*tmp = '\0';
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++;
}
return 0;
}
bool ClearString(HString &Str)
{
if (Str.ch)
delete Str.ch;
Str.ch = new char[1];
*Str.ch = '\0';
Str.length = 0;
return true;
}
bool DestoryString(HString &Str)
{
if (Str.ch)
delete Str.ch;
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;
}
|