用一组地址连续的存储空间来存储单元存放串值字符序列,但是他们的存储空间是在程序执行的过程中动态分配而获得的,结构体的定义如下:
typedef struct
{
char *ch;
int length;
}
在这个基础上我们实现对串的一些基本的操作,比如说,初始化一个字符串,求串长字符串的比较,清空字符串,字符串的连接,求子字符串。
#include<iostream>
#include<stdio.h>
using namespace std;
typedef int Status;
#define OVERFLOW -2
#define OK 1
#define ERROR 0;
typedef struct{
char *ch;//如果字符串是非空的,则按照串长来分配存储区,否则为ch为null
int length;
}HString;
Status strAssign(HString *T,char chars[])
{
int i=0,j=0;
for(i=0;chars[i]!='#';i++){}
if(!i)
{
T->ch=NULL;
T->length=0;
}
else
{
if(!(T->ch=(char*)malloc(i*sizeof(char))))
{
exit(OVERFLOW);
}
for(j=0;j<i;j++)
T->ch[j]=chars[j];
T->length=i;
}
return OK;
}
int getLength(HString *T)
{
return T->length;
}
Status showString(HString *T)
{
if(!T->ch)
{
return ERROR;
}
for(int i=0;i<T->length;i++)
cout<<T->ch[i]<<" ";
return OK;
}
Status ClearString(HString *T)
{
if(T->ch)
{
T->ch=NULL;
}
T->length=0;
return OK;
}
Status strCompare(HString *S,HString *T)
{
//这个函数的作用是比较两个字符串,如果S>T就返回>0,如果是S=T就返回0,
//如果是S<T就返回<0
int i=0;
for(i;i<S->length && i<T->length;i++)
{
if(S->ch[i]!=T->ch[i])
return S->ch[i]-T->ch[i];
}
return S->length-T->length;
}
Status strConcat(HString *S,HString *S1,HString *S2)
{
//用S返回S1和S2两个字符串连接的结果
if(S->ch)
{
S->ch=NULL;
}
if(!(S->ch=(char*)malloc((S1->length+S2->length)*sizeof(char))))
{
exit(OVERFLOW);
}
int i=0;
for(i;i<S1->length;i++)
{
S->ch[i]=S1->ch[i];
}
for(i;i<S1->length+S2->length;i++)
{
S->ch[i]=S2->ch[i-S1->length];
}
S->length=S1->length+S2->length;
return OK;
}
Status subString(HString *sub,HString *S,int pos,int len)
{
int i=0;
//利用sub返回串S中的第pos个字符起的长度的len的子串
if(pos<1 || pos>S->length || len<0 || len>S->length-1)
return ERROR;
if(sub->ch) sub->ch=NULL;
if(!len)
{
sub->ch=NULL;
sub->length=0;
}
else
{
sub->ch=(char *)malloc(len*sizeof(char));
for(i;i<len;i++)
sub->ch[i]=S->ch[pos+i];
sub->length=len;
}
return OK;
}
int main()
{
HString S;
HString T,sub;
char chars[100];
int i=0;
int length;
while(1)
{
cout<<"********************************"<<endl;
cout<<"********0.初始化一个字符串******"<<endl;
cout<<"********1.获取字符串的长度******"<<endl;
cout<<"********2.显示字符串***********"<<endl;
cout<<"********3.清空字符串************"<<endl;
cout<<"********4.字符串做比较**********"<<endl;
cout<<"********5.字符串连接************"<<endl;
cout<<"********6.求子串****************"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 0:
cout<<"请输入一串字符'#'结束输入:"<<endl;
cin>>chars[i];
while(chars[i]!='#')
{
i++;
cin>>chars[i];
}
strAssign(&S,chars);
break;
case 1:
length=getLength(&S);
cout<<"字符串的长度为:"<<length<<endl;
break;
case 2:
showString(&S);
cout<<endl;
break;
case 3:
ClearString(&S);
break;
case 4:
i=0;
cout<<"请输入新字符串:"<<endl;
cin>>chars[i];
while(chars[i]!='#')
{
i++;
cin>>chars[i];
}
strAssign(&T,chars);
cout<<strCompare(&S,&T)<<endl;
break;
case 5:
HString S1,S2;
i=0;
*chars=NULL;
cout<<"请输入第一个字符串S1: "<<endl;
cin>>chars[i];
while(chars[i]!='#')
{
i++;
cin>>chars[i];
}
strAssign(&S1,chars);
i=0;
cout<<"请输入第二个字符串S2: "<<endl;
*chars=NULL;
cin>>chars[i];
while(chars[i]!='#')
{
i++;
cin>>chars[i];
}
strAssign(&S2,chars);
strConcat(&S,&S1,&S2);
break;
case 6:
int pos,len;
cout<<"请输入从何处开始求子串(在主串中的位置):"<<endl;
cin>>pos;
cout<<"请输入求你所要求的子串的长度:"<<endl;
cin>>len;
subString(&sub,&S,pos-1,len);
cout<<"所求的子串为:"<<endl;
showString(&sub);
cout<<endl;
break;
default:
break;
}
}
return 0;
}