51c语言字符,c语言实现字符串的各种操作

以下是我用c语言实现数据结构中字符串的各种操作#pragma once

#ifndef _STDLIB_H

#define _STDLIB_H

#include 

#endif

#ifndef _SIXE_H

#define _SIZE_H

#define SIZE              100

#endif

#ifndef _STRING_H

#define _STRING_H

typedef struct

{

char * p_ch;

long Length;

}String;

#endif

#include "stdafx.h"

bool Str(String &T);

bool StrAssign(String &T,char * p_char);

bool StrEmpty(const String &T);

int GetLength(const String &T);

int StrCompare(const String &S,const String &T);

bool StrClear(String &T);

bool StrConcat(String &T,String S1,String S2);

bool SubString(const String &T,int Pos,int Len,String &S);

char GetElem(const String &T,int Pos);

bool Find(const String &T,const String &S);

bool StrCopy(String &T,String S);

int Index(const String &T,const String &S);

bool StrInsert(String &T,int Pos,String S);

bool StrDelete(String &T,int Pos,int Len);

char * GetBuffer(const String &T);

#include "String.h"

bool Str(String &T)

{

T.p_ch=NULL;

T.Length=0;

return true;

}

bool StrAssign(String &T,char * p_char)

{

int Len=0;

char * temp=p_char;

while(* temp!='\0')

{

Len++;

temp++;

}

if(T.Length!=0)

free(T.p_ch);

{

T.p_ch=(char *)malloc((Len+1)*sizeof(char));

if(T.p_ch==NULL)

return false;

else

{

int i=0;

for(i=0;i

{

T.p_ch[i]=p_char[i];

}

T.p_ch[i]='\0';

T.Length=Len;

return true;

}

}

}

bool StrEmpty(const String &T)

{

if(T.Length==0)

return true;

else

return false;

}

int GetLength(const String &T)

{

return T.Length;

}

int StrCompare(const String &S,const String &T)

{

if(GetLength(S)==GetLength(T))

{

int i;

for(i=0;i

{

if(S.p_ch[i]

{

return -1;

}

if(S.p_ch[i]>T.p_ch[i])

{

return 1;

}

i++;

}

return 0;

}

else

{

int i;

for(i=0;i

{

if(S.p_ch[i]

{

return -1;

}

if(S.p_ch[i]>T.p_ch[i])

{

return 1;

}

i++;

}

if(GetLength(S)>GetLength(T))

return 1;

else

return -1;

}

}

bool StrClear(String &T)

{

if(T.Length==0)

return false;

else

{

free(T.p_ch);

T.p_ch=NULL;

T.Length=0;

return true;

}

}

bool StrConcat(String &T,String S1,String S2)

{

if(T.Length!=0)

free(T.p_ch);

T.p_ch=(char *)malloc((S1.Length+S2.Length+1)*sizeof(char));

if(T.p_ch==NULL)

return false;

else

{

int i=0,j=0;

for(i=0;i

{

T.p_ch[i]=S1.p_ch[i];

}

for(j=0;j

{

T.p_ch[i]=S2.p_ch[j];

}

T.p_ch[i]='\0';

T.Length=S1.Length+S2.Length;

return true;

}

}

bool SubString(const String &T,int Pos,int Len,String &S)

{

if(Pos+Len>T.Length)

return false;

else

{

if(S.Length!=0)

free(S.p_ch);

else

{

S.p_ch=(char *)malloc((Len+1)*sizeof(char));

int i,temp=Pos+Len;

for(i=0;Pos

{

S.p_ch[i]=T.p_ch[Pos];

}

S.p_ch[i]='\0';

S.Length=Len;

return true;

}

}

}

char GetElem(const String &T,int Pos)

{

if(Pos>=T.Length)

return NULL;

else

{

return T.p_ch[Pos];

}

}

bool StrCopy(String &T,String S)

{

if(S.Length==0)

return false;

else

{

T.p_ch=(char *)malloc((S.Length+1)*sizeof(char));

int i=0;

while(i

{

T.p_ch[i]=S.p_ch[i];

i++;

}

T.p_ch[i]='\0';

T.Length=S.Length;

return true;

}

}

bool StrInsert(String &T,int Pos,String S)

{

if(S.Length==0||Pos>=T.Length)

return false;

else

{

int i,j;

String temp;

Str(temp);

StrCopy(temp,T);

free(T.p_ch);

T.p_ch=(char *)malloc((temp.Length+S.Length+1)*sizeof(char));

if(T.p_ch==NULL)

return false;

else

{

for(i=0;i<=Pos;i++)

{

T.p_ch[i]=temp.p_ch[i];

}

Pos++;

for(j=0;j

{

T.p_ch[i]=S.p_ch[j];

i++;

}

while(Pos

{

T.p_ch[i]=temp.p_ch[Pos];

i++;

Pos++;

}

T.p_ch[i]='\0';

T.Length=temp.Length+S.Length;

free(temp.p_ch);

return true;

}

}

}

bool StrDelete(String &T,int Pos,int Len)

{

if(Pos+Len>T.Length)

return false;

else

{

String temp;

StrCopy(temp,T);

free(T.p_ch);

int i,j;

T.p_ch=(char *)malloc((temp.Length-Len+1)*sizeof(char));

if(T.p_ch==NULL)

return false;

else

{

for(i=0;i

{

T.p_ch[i]=temp.p_ch[i];

}

for(j=Pos+Len;j

{

T.p_ch[i]=temp.p_ch[j];

}

T.p_ch[i]='\0';

T.Length=temp.Length-Len;

free(temp.p_ch);

}

}

}

char * GetBuffer(const String &T)

{

return T.p_ch;

}

bool Find(const String &T,const String &S)

{

int posT=0,posS=0;

int lengthT=T.Length,lengthS=S.Length;

while(posT

{

if(S.p_ch[posS]==T.p_ch[posT])

{

posT++;

posS++;

}

else

{

if(posS==0)

{

posT++;

}

else

{

if(S.p_ch[0]==T.p_ch[posS])

{

posS=1;

posT++;

}

else

{

posS=0;

posT++;

}

}

}

}

if(posS==lengthS)

{

return true;

}

else

{

return false;

}

}

int Index(const String &T,const String &S)

{

int posT=0,posS=0;

int lengthT=T.Length,lengthS=S.Length;

while(posT

{

if(S.p_ch[posS]==T.p_ch[posT])

{

posT++;

posS++;

}

else

{

if(posS==0)

{

posT++;

}

else

{

if(S.p_ch[0]==T.p_ch[posS])

{

posS=1;

posT++;

}

else

{

posS=0;

posT++;

}

}

}

}

if(posS==lengthS)

{

return posT-S.Length;

}

else

{

return -1;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值