数据结构 串的基本操作(顺序存储方式)
//采用定长顺序存储来表示串的基本操作#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 225
typedef struct
{
char str[MAXSIZE];
int length;
} SeqString;
int StrInsert(SeqString *S, int i, SeqString T)
{
int k;
if(i > S->length || S->length + T.length > MAXSIZE)
{
printf("不能插入\n");
return 0;
}
else
{
for(k = S->length - 1; k >= i - 1; k--)
{
S->str [k + T.length ] = S->str [k];
}
for(k = 0; k < T.length ; k++)
S->str [i + k - 1] = T.str [k];
S->length += T.length ;
S->str [S->length ] = '\0';
}
printf("插入之后的字符串:");
printf("%s\n", S->str );
return 0;
}
int StrDelete(SeqString *S, int i, int len)
{
int k;
if(i < 1 || i > S->length || i - 1 + len > S->length)
return 0;
else
{
for(k = i - 1 + len; k < S->length; k++)
S->str[k - len] = S->str[k];
S->length = S->length - len;
S->str[S->length] = '\0';
}
printf("删除之后的字符串:");
printf("%s\n", S->str);
return 0;
}
int StrLength(SeqString S)
{
int i = 0;
while(S.str [i] != '\0')
++i;
S.length = i;
printf("字符串的长:%d\n", S.length );
return 0;
}
int Concat(SeqString s1, SeqString s2)
{
int i;
SeqString S;
if(s1.length + s2.length > MAXSIZE)
{
return 0;
}
else
{
for(i = 0; i < s1.length; i ++)
S.str [i] = s1.str [i];
for(i = 0; i < s2.length; i ++)
S.str [s1.length + i] = s2.str [i];
S.length = s1.length + s2.length ;
S.str [S.length ] = '\0';
}
printf("连接后的字符串:%s\n", S.str);
return 0;
}
int SubString(SeqString S, int p, int len)
{
int i;
SeqString T;
if(S.length <= 0 || p > S.length || len < 0 || len > S.length - p + 1)
return 0;
else
{
for(i = 0; i < len; i ++)
T.str [i] = S.str [p + i - 1];
T.str [len] = '\0';
}
printf("取得的字串:");
printf("%s\n",T.str );
return 0;
}
int main(void)
{
SeqString s1, s2, s3;
int i, length;
printf("字符串操作基本练习:\n");
printf("* * * * * * * * * * * * * *\n");
printf("1-------------求串长\n");
printf("2-------------串插入\n");
printf("3-------------串连接\n");
printf("4-------------子串删除\n");
printf("5-------------取子串\n");
printf("0-------------退出\n");
printf("* * * * * * * * * * * * * *\n");
i = 5;
while(i < 6)
{
printf("请选择操作类型:");
scanf("%d", &i);
switch(i)
{
case 1: printf("请输入串:");
scanf("%s", s1.str );
StrLength(s1);
printf("请继续选择\n");
break;
case 2: printf("插入串是:");
scanf("%s", s2.str );
printf("字符串长度为:%d\n", s2.length = strlen(s2.str ));
printf("要插入的字符串:");
scanf("%s", s3.str );
printf("字符串的长度为:%d\n", s3.length = strlen(s3.str ));
printf("输入要插入的位置:");
scanf("%d", &i);
StrInsert(&s2, i, s3);
printf("请继续选择\n");
break;
case 3:printf("请输入前串:");
scanf("%s", s1.str );
printf("字符串长度为:%d", s1.length = strlen(s1.str ));
printf("请输入后串:");
scanf("%s", s2.str );
printf("字符串长度为:%d", s2.length = strlen(s2.str ));
Concat(s1, s2);
printf("请继续选择\n");
break;
case 4:printf("请输入源子串:");
scanf("%s", s1.str );
printf("字符串长度为:%d", s1.length = strlen(s1.str ));
printf("请输入要删除的位置:");
scanf("%d", &i);
printf("请输入删除的长度:");
scanf("%d", &length);
StrDelete(&s1, i, length);
printf("请继续选择\n");
break;
case 5:printf("请输入源子串:");
scanf("%s", s1.str );
printf("字符串长度为:%d\n", s1.length = strlen(s1.str ));
printf("请输入子串的位置:");
scanf("%d", &i);
printf("请输入子串的长度:");
scanf("%d", &length);
SubString(s1, i, length);
printf("请继续选择\n");
break;
case 0: printf("程序结束\n");
exit(0);
}
}
return 0;
}
c语言实现,给大家分享下。