[데이터 구조] 문자열 의 기본 조작
9618 단어 알고리즘 서론 과 데이터 구조
/*
==========================================================================================
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, T, V)
: S,T V ,T
: V S T
11.StrInsert(&S, pos, T)
: S T ,1≦pos≦Strlength(S)+1
: S pos T
12.StrDelete(&S, pos, 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
#include
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() , ,
");
system("pause");
exit(-1);
}
char *tmp = T.ch;
while(*chars)
{
*tmp++ = *chars++;
}
*tmp = '\0';
printf("StrAssign() , T
");
return true;
}
//2.StrCopy(&T, S)
bool StrCopy(HString &T, HString Str)
{
if (!Str.ch)
{
printf("StrCopy() , ,
");
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() , ,
");
system("pause");
exit(-1);
}
char *tmp = T.ch;
while(*Str.ch)
{
*tmp++ = *Str.ch++;
}
*tmp = '\0';
printf("StrCopy ,
");
return true;
}
//3.StrEmpty(S)
bool StrEmpty(HString Str)
{
if (!Str.ch)
{
printf("StrEmpty() , ,
");
system("pause");
exit(0);
}
else
{
if (!Str.length)
{
printf("StrEmpty() ,
");
return true;
}
else
{
printf("StrEmpty() ,
");
return false;
}
}
}
//4.StrCompare(S, T)
int StrCompare(HString Str, HString T)
{
if ((!T.ch)||(!Str.ch))
{
printf("StrCompare() ,
");
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
", flag);
return flag;
}
//5.StrLength(S)
int Strlength(HString Str)
{
if (!Str.ch)
{
printf("Strlength() , ,
");
system("pause");
exit(0);
}
printf("Strlength() , : %d
", Str.length);
return Str.length;
}
//6.ClearString(&S)
bool ClearString(HString &Str)
{
if (Str.ch)
{
delete[] Str.ch;
}
else
{
printf("ClearString() , ,
");
system("pause");
exit(0);
}
Str.ch = new char[1];
*Str.ch = '\0';
Str.length = 0;
printf("ClearString() ,
");
return true;
}
//7.Concat(&T, S1, 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() , ,
");
system("pause");
exit(-1);
}
char *tmp = T.ch;
while(*S1.ch)
{
*tmp++ = *S1.ch++;
}
while(*S2.ch)
{
*tmp++ = *S2.ch++;
}
*tmp = '\0';
printf("Concat() ,
");
return true;
}
//8.SubString(&Sub, S, pos, len)
bool SubString(HString &Sub, HString Str, int pos, int len)
{
if (Sub.ch)
{
delete[] Sub.ch;
}
if (!Str.ch)
{
printf("SubString() , ,
");
system("pause");
exit(0);
}
if ((pos < 1)||(pos > Str.length)||(len < 0)||(len > Str.length - pos +1))
{
printf("SubString() , pos len ,
");
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() ,
");
return true;
}
//9.Index(S, T, pos)
int Index(HString Str, HString T, int pos)
{
if (!Str.ch||!T.ch||(0 == T.length))
{
printf("Index() , ,
");
system("pause");
exit(0);
}
if ((pos < 1)||(pos > Str.length))
{
printf("Index() ,pos
");
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
", pos, i);
return i;
}
}
Str.ch++;
}
printf("Index() , %d T
", pos);
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() , ,
");
system("pause");
exit(0);
}
int pos = Index(Str, T, 1);
while(pos)
{
int nlength = Str.length - T.length + V.length;
char *ctemp = new char[nlength + 1];
if (!ctemp)
{
printf("Replace() , ,
");
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, T, pos + V.length);
}
printf("Replace() ,
");
return true;
}
//11.StrInsert(&S, pos, T)
bool StrInsert(HString &Str, int pos, HString T)
{
if ((pos < 1)||(pos > Str.length + 1)||(NULL == T.ch))
{
printf("StrInsert() ,pos
");
return false;
}
int nlength = Str.length + T.length;
char *ctemp = new char[nlength + 1];
if (!ctemp)
{
printf("StrInsert() , ,
");
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
", pos);
return true;
}
//12.StrDelete(&S, pos, len)
bool StrDelete(HString &Str, int pos, int len)
{
if ((pos < 1)||(pos > Str.length - len + 1))
{
printf("StrDelete() ,
");
return false;
}
int nlength = Str.length - len;
char *ctemp = new char[nlength + 1];
if (!ctemp)
{
printf("StrDelete() , ,
");
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
", pos, len);
return true;
}
//13.DestroyString(&S)
bool DestoryString(HString &Str)
{
if (Str.ch)
{
delete[] Str.ch;
}
Str.ch = NULL;
Str.length = 0;
printf("DestoryString() ,
");
return true;
}
//14.PrintString(T)
bool PrintString(HString T)
{
if (!T.ch)
{
printf("PrintString() ,
");
return false;
}
else
{
printf("PrintString() , %d , :", T.length);
while(*T.ch)
{
printf("%c", *T.ch++);
}
printf("
");
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;
}