#include
#include
#include
#define TRUE 1
#define FALSE 0
#define OVERFLOW 1
#define MAX_SIZE 1024
typedef int Status;
/**
*串的顺序结构实现
typedef struct{
char ch[MAX_SIZE+1];
int length;
}SeqString;
*/
/**串的堆式顺序存储结构(Heap)*/
typedef struct{
char * ch ;
int length;
}HeapString;
void InitString(HeapString * str)
{
str->ch = NULL;
str->length = 0;
}
/**为串Str赋值*/
Status StrAssigh(HeapString *str,char * chars)
{
int len = strlen(chars);
InitString(str);
str->ch = (char*)malloc(len * sizeof(char));
if(!str->ch)
exit(OVERFLOW);
for(int i = 0;i < len;i++)
{
str->ch[i] = chars[i];
}
str->length = len;
return 1;
}
void PrintString(HeapString * str)
{
if(str->length == 0)
printf("字符串为空!\n");
for(int i = 0;i < str->length;i++)
{
printf("%c",str->ch[i]);
}
}
Status StrCopy(HeapString * destStr,HeapString * srcStr)
{
InitString(destStr);
destStr->ch = (char *)malloc(sizeof(char) * srcStr->length);
for(int i = 0;i < srcStr->length;i++)
{
destStr->ch[i] = srcStr ->ch[i];
}
destStr->length = srcStr->length;
return TRUE;
}
/**比较两个字符串的大小str1 == str2 返回0 ;str1 < str2 返回负数;str1 > str2 返回正数*/
Status StrCompare(HeapString * str1,HeapString * str2)
{
for(int i = 0;i < str1->length && i < str2->length;i++)
{
//相等的字符跳过,直到不等的字符
if(str1->ch[i] != str2->ch[i])
return str1->ch[i] - str2->ch[i] ;//返回正数和负数
}
//字符都相等,比较字符串的长度
return str1->length - str2->length;
}
Status Concat(HeapString *destStr,HeapString *str1,HeapString *str2)
{
InitString(destStr);
int len = str1->length + str2 ->length;
destStr->ch = (char*)malloc(sizeof(char) * len);
for(int i = 0;i < str1->length;i++)
{
destStr->ch[i] = str1->ch[i];
}
for(int i = 0;i< str2->length;i++)
{
destStr->ch[i+str1->length] = str2->ch[i];
}
destStr->length = len;
return TRUE;
}
/**截取str字符串从pos位置len长度的字符到deststr中*/
Status SubString(HeapString * deststr,HeapString *str,int pos,int len)
{
InitString(deststr);
deststr->ch = (char *)malloc(sizeof(char) * len);
for(int i = 0;i < len;i++)
{
deststr->ch[i] = str->ch[i+pos-1];
}
deststr->length = len;
return TRUE;
}
/**返回子串child在父串parent中的位置*/
int Index(HeapString * parent,HeapString * child)
{
HeapString * str = (HeapString*)malloc(sizeof(HeapString));
InitString(str);
int len = child->length;
int pos = 0;
while(pos + len <= parent ->length)
{
SubString(str,parent,pos+1,len);
if(!StrCompare(str,child))
return pos+1;
pos++;
}
return FALSE;
}
/**从pos位置删除len个字符*/
Status StrDelete(HeapString * str,int pos ,int len)
{
for(int i = pos-1;i < str->length - len;i++)
str->ch[i] = str->ch[i+len];
str->length = str->length - len;
return TRUE;
}
/**在str中的pos位置插入字符串insertstr*/
Status StrInsert(HeapString * str ,int pos,HeapString * insetstr)
{
int len = str->length + insetstr->length;
str -> ch = (char*)realloc(str->ch,sizeof(char)*len);
for(int i = len-1;i >= pos+insetstr->length-1 ;i--)
{
str->ch[i] = str->ch[i-1];
}
for(int i = 0;i < insetstr->length;i++)
{
str->ch[i + pos -1] = insetstr->ch[i];
}
str->length = len;
return TRUE;
}
/**将str中的oldstr字符串替换成newstr字符串*/
Status Replace(HeapString *str,HeapString *oldstr,HeapString *newstr)
{
int pos = Index(str,oldstr);
StrDelete(str,pos,oldstr->length);
StrInsert(str,pos,newstr);
return TRUE;
}
int main()
{
char chars[] = "abcdefg";
HeapString * str1 = (HeapString *)malloc(sizeof(HeapString));
HeapString * str2 = (HeapString *)malloc(sizeof(HeapString));
HeapString * str3 = (HeapString *)malloc(sizeof(HeapString));
HeapString * str4 = (HeapString *)malloc(sizeof(HeapString));
HeapString * str5 = (HeapString *)malloc(sizeof(HeapString));
printf("str1:");
StrAssigh(str1,chars);
PrintString(str1);
StrCopy(str2,str1);
printf("\n复制后的str2:");
PrintString(str2);
StrAssigh(str3,"abccdesdf");
printf("\nstr1与str3比较结果为:%d",StrCompare(str1,str3));
Concat(str4,str1,str3);
printf("\nstr1与str3连接后的字符串为:");
PrintString(str4);
SubString(str5,str1,2,3);
printf("\n从str1中截取的字符串为:");
PrintString(str5);
printf("\nstr5在str1中的位置为:%d",Index(str1,str5));
printf("\nstr1中从第2个位置删除3个字符:");
StrDelete(str1,2,3);
PrintString(str1);
HeapString * str6 = (HeapString *)malloc(sizeof(HeapString));
StrAssigh(str6,"ABCD");
printf("\nstr1中从第3个位置插入字符串ABCD:");
StrInsert(str1,3,str6);
PrintString(str1);
printf("\nstr2中字符串bcd(str5)替换成str6:");
Replace(str2,str5,str6);
PrintString(str2);
return 0;
}