数据结构与算法中~使用顺序存储实现串的基本操作

数据结构与算法中串的顺序表实现

#include<stdlib.h>
#include<stdio.h>
typedef struct {
	char *ch;
	int length;
}heapstring;

void Str_Assign(heapstring *s){ 
	s->ch=(char*)malloc(100*sizeof(char));
	printf("\n请输入你串的内容:\n");
	gets(s->ch);
}

void PrintString(heapstring *s){
	int i=0;
	if(!s->ch) printf("\n串是空的!");
	else{
		printf("\n串的内容是:");
		while(s->ch[i]!='\0'){
			printf("%c",s->ch[i]);
			i++;
		}
		printf("\t串的长度是%d.",s->length);
		printf(" =====输出结束.");
	}
}

int MenuShow(){
	int e;
	printf("\n请你选择:1-串1输出  2-串2输出  3-串比较  4-串复制  5-串连接  6-求子串(exit for 0):");
	scanf("%d",&e);
	return e;
}

void StrCopy(heapstring *s0,heapstring *t){
	int i=0;
	if(!(s0->ch=(char*)malloc(100*sizeof(char)))){ 
		printf("\n容量满了!");
		exit(0);
    }
	s0->length =t->length;
	while (i<t->length)
		s0->ch[i] =t->ch[i++];
	s0->ch[i]='\0';
}

int StrCompare(heapstring *s1,heapstring *s2){
	int i;
	if (!s1->length||!s2->length) printf("\n串是空的!");
	else{
		for(i=0;i<s1->length&&i<s2->length;++i)
       if (s1->ch[i]!=s2->ch[i])  
		   return (s1->ch[i]-s2->ch[i]);
	   return (s1->length-s2->length);
     }
}

void StrConcat(heapstring *s0,heapstring *s1,heapstring *s2){ 
	int i,j;
	if(!(s0->ch=(char*)malloc(s0->length*sizeof(char)))){ 
		printf("\n容量满了!");
		exit(0);
    }else{  
		for(i=0;s1->ch[i]!='\0';i++) 
			s0->ch[i]=s1->ch[i];
		for(j=0;s2->ch[j]!='\0';j++) 
			s0->ch[i+j]=s2->ch[j];
		s0->ch[i+j]='\0';
      }
}

int StrLen(heapstring *s){
	int len=0;
	while(s->ch[len]!='\0')  
		len++;
	return len;
}

void SubStr(heapstring *s,int pos,int len){
 heapstring sub;
 int i;
 if(pos<1||pos>s->length||len<0||len>s->length-pos+1)
    printf("error!");
 else { 
       if (!len){
		   sub.ch=NULL;
		   sub.length=0;
          }
	else {
              sub.ch=(char*)malloc(len*sizeof(char));
			  for(i=0;i<len;i++)  
				  sub.ch[i]=s->ch[pos-1+i];
              sub.ch[i]='\0';
              sub.length=len;
             }
        PrintString(&sub);
      }
}

main(){
	int e,result,k;
	int pos,len;
	heapstring s1,s2,s0,s3;
	Str_Assign(&s1);
	s1.length=StrLen(&s1);
	Str_Assign(&s2);
	s2.length=StrLen(&s2);
	e=MenuShow();
	while(e!=0){ 
		switch (e){
		case 1:
			PrintString(&s1);
			break;
		case 2:
			PrintString(&s2);
			break;
		case 3:
			result=StrCompare(&s1,&s2);
			if (result) 	printf("\n串不相等");
			else printf("\n串相等");
			break;
		case 4:
			StrCopy(&s0,&s1);
			printf("\n串的复制结果是:");
			PrintString(&s0);
			break;
		case 5:
			s0.length=s1.length+s2.length;
			StrConcat(&s0,&s1,&s2);
			PrintString(&s0);
			break;
		case 6:
			printf("\n请输入子串的起始位置和长度:");
			scanf("%d%d",&pos,&len);
			SubStr(&s1,pos,len);
			break;
		default:printf("\n输入内容错误!");
		}
		e=MenuShow();
	}
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值