#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAXSIZE 10
#define DataType char
typedef struct
{
DataType ch[MAXSIZE];
int length;
}SString;
SString *CreatString(void);
int GetLength(SString *S);
int SStrInsert(SString *S,int pos,const SString *T);
int SStrDelete(SString *S,int pos,int len);
int SStrCat(SString *S,const SString *T);
int SubSString(SString *T,SString *S,int pos,int len);
void PrintString(SString *S);
void Destroy(SString *S);
SString *CreatString(void)
{
SString *S=NULL;
S=(SString *)malloc(sizeof(SString));
S->length=0;
scanf("%s",S->ch);
S->length=GetLength(S);
return S;
}
int GetLength(SString *S)
{
int i=0;
while(S->ch[i]!='\0')
{
i++;
}
return i;
}
int SStrInsert(SString *S,int pos,const SString *T)
{
int length_T=GetLength(T);
int length_S=GetLength(S);
int i=0;
int j=0,k=0,p=0,q=0;
if(pos<=0||pos>=MAXSIZE)
{
printf("Illegal insert location.\n");
return 1;
}
else
{
i=pos-1+length_T;
k=pos-1;
if(length_S+length_T<=MAXSIZE)
{
p=length_S-pos+1;
q=length_T;
}
else if(length_T+pos<=MAXSIZE)
{
q=length_T;
p=MAXSIZE-length_T-pos+1;
}
else
{
q=MAXSIZE-pos+1;
p=0;
}
for(j=0;j<p;j++)
{
S->ch[i+p-1]=S->ch[k+p-1];
i--;
k--;
}
i=pos-1;
k=0;
for(j=0;j<q;j++)
{
S->ch[i]=T->ch[k];
i++;
k++;
}
S->length=GetLength(S);
}
return 0;
}
int SStrDelete(SString *S,int pos,int len)
{
int i=0;
int length=0;
int k=0;
if(pos<=0 || pos>GetLength(S) || NULL==S || len<=0 || len>GetLength(S))
{
printf("Illeagel input.The program ends.\n");
exit(0);
}
else
{
if(pos+len<=MAXSIZE)
length=len;
else
{
length=GetLength(S)-pos-1;
}
k=pos-1;
for(i=0;i<length;i++)
{
S->ch[k]=S->ch[k+len];
k++;
}
S->length=GetLength(S);
return 0;
}
}
int SStrCat(SString *S,const SString *T)
{
int pos=GetLength(S);
int i=0;
for(i=0;i<GetLength(T);i++)
{
if(pos==MAXSIZE)
break;
else
{
S->ch[pos]=T->ch[i];
pos++;
}
}
S->length=GetLength(S);
return 0;
}
int SubSString(SString *T,SString *S,int pos,int len)
{
int i=0;
int k=pos-1;
if(pos<=0 || pos>=GetLength(S) || NULL==S || len<=0 || len>GetLength(S))
{
printf("Illeagel input.The program ends.\n");
exit(0);
}
else
{
for(i=0;i<len;i++)
{
if(k==GetLength(S))
{
break;
}
else
{
printf("%c ",S->ch[k]);
k++;
}
}
return 0;
}
}
void PrintString(SString *S)
{
int i=0;
while(S->ch[i]!='\0')
{
printf("%c",S->ch[i]);
i++;
}
printf("\n");
}
void Destroy(SString *S)
{
free(S);
S=NULL;
}
int main()
{
SString *s,*t;
SString *t_2,*t_3=NULL ;
printf("Please Input a string ,the max length is %d.\n",MAXSIZE);
s=CreatString();
printf("Please Input a waiting for being inserted string.\n");
t=CreatString();
SStrInsert(s,5,t);
printf("After insert a string,the new string is:\n");
PrintString(s);
SStrDelete(s,4,4);
printf("After delect a string,the new string is:\n");
PrintString(s);
printf("Please input a string that needs to be chained.\n");
t_2=CreatString();
SStrCat(s,t_2);
printf("After chaining the two strings,the new strings is:\n");
PrintString(s);
SubSString(t_3,s,3,4);
Destroy(s);
return 0;
}
串的基本用法(顺序存储结构)
最新推荐文章于 2022-07-10 10:24:58 发布