博客
关于我
串的基本操作实现源码|数据结构
阅读量:325 次
发布时间:2019-03-04

本文共 3498 字,大约阅读时间需要 11 分钟。

typedef struct {   	char *ch;	int length;}HStirng;
ADT{   	StrAssign(&T,chars);	//chars是字符串常量	//生成一个值等于chars的串T	strCopy(&T,S)	//串S存在	//由串S复制得串T		StrEmpty(S)	//串S存在	//若为空则返回true	StrCompare(S,T)	//S>T返回值大于0	//S
bool StrAssign(&T,chars){   	//delete[] 释放new分配的对象数组指针指向的内存	if(T.ch)	{   		delete[] T.ch;	}	char *ctemp = chars;	T.length=0;	//求T.length的长度	while(*ctemp)	{   		++T.length;		++ctemp;	}	T.ch = new char[T.length+1];		if(!T.ch)	{   		printf("分配失败!");		system(pause);		exit(-1);	}	else	{   		char *temp = T.ch;		while(*chars)		{   			*temp++ = *chars++;		}		printf("分配成功!");		return true;	}
bool StrCopy(&T,S){   	if(!S.ch)	{   		printf("串S不存在!");		system(pause);   		 exit(0);	}		 if(T.ch)	 {   	  delete[] T.ch;	 }	 	T.length = S.length;	T.ch = new char[S.length+1];		if(!T.ch)	 {   	  printf("分配失败!");	  system(pause);	  exit(-1);	 }		char *temps = S.ch;	char *tempt = T.ch;		while(*temps)	{   		*tempt++ = *temps++;	}	*tempt = '\0';		printf("成功!");	return true;}
bool StrEmpty(S){   	  if(!S.ch)	  {   	   printf("串不存在!\n");	   system(pause);	   exit(0);	  }	 else	 {   	 	if(!S.length)	 	{   	 		printf("串为空!\n");	 		return true;	 	}	 	else	 	{   		    printf("串不为空!\n");		    return false;		}	 }	}

在这里插入图片描述

int StrLength(S){      if(!S.ch)   {   	    printf("串不存在!\n");	    system(pause);	    exit(0);   }  else  {     	    printf("串的长度%d",S.length);  	    return S.length;  }

在这里插入图片描述

注意清空串跟一个串不存在的区别!

清空是ch="\0",length=0,但给他分配了内存空间
串不存在是没有分配内存空间

bool Contact(&T,S1,S2){   	if(!T.ch)	{   	     printf("串不存在!\n");             system(pause);             exit(0);	}	T.length = S1.length+S2.length;	T.ch = new char[T.length+1];	if(!T.ch)	{   	     printf("分配失败!\n");             system(pause);             exit(-1);	}	char *temp = T.ch;	while(*S1.ch)	{   		*temp++ = *S1.ch++;	}		while(*S2.ch)	{   	       *temp++ = *S2.ch++;	}		*temp = '\0';	printf("连接成功!\n");	return true;}
bool SubString(&Sub,S,pos,len){   	if(!S.ch)	{   	     printf("串不存在!\n");             system(pause);             exit(0);	}	if(Sub.ch)	{   	     delete[] Sub.ch;	}	if(Pos<1 || Pos>S.length || len<0 || len>S.length-Pos+1 )	{   		printf("Pos&&len 错误!");		Sub.ch = new char[1];		*Sub.ch="\0";		Sub.length = 0;		return false;	}	Sub.ch = new char[len+1];	char *temp = Sub.ch;	for(int i=1;i != Pos;i++)	{   		S.ch++;	}	for(i=0;i!=len;i++)	{   		Sub.ch++ = S.ch++;	}	return true;}
bool Index(S,T,pos){   	if(!S.ch || !T.ch)	{   	     printf("串不存在!\n");             system(pause);             exit(0);	}	if(Pos <= 1 || Pos >= S.length)	{   	    printf("Pos错误!");	}	else	{   		char *tempt = T.ch;	  	char *temps = S.ch;		for(int i=0;i
T.length) { printf("匹配成功"); return tempt.length-temps.length; } else return false;}
bool StringReplace(HStirng &Str,HStirng T,HStirng V){   	if(!Str.ch || !T.ch || !V.ch || 0 == T.length)	{   		printf("串不存在或为空!");		system(pause);		exit(0);	} 	int pos = Index(S,T,1);	while(pos)	{   		int nlength = Str.lenght + V.length - T.length;		char *ctemp = new char[nlength+1];		if(!ctemp)		{   			printf("函数执行失败!");			system(pause);                        exit(-1);		}		char *temp = ctemp;		char *stemp = Str.ch;		char *vtemp = V.ch;		for(int i=0;i != pos;i++)		{   			*temp++ = *stemp++;		}		for(i=0;i!=T.length;i++)		{   			stemp++;		}		for(i=0;i!=V.length;i++)		{   			*temp++ = *vtemp++;		}		while(*stemp)		{   			*temp++ = *stemp++;		}		*temp='\0';		delete Str.ch;		Str.length = nlength;		Str.ch  = temp;		pos = Index(S,T,Pos+V.length);	}	printf("子串替代成功");	return true;}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

转载地址:http://ulsh.baihongyu.com/

你可能感兴趣的文章
水调歌头·1024
查看>>
对不起
查看>>
C++ 函数重载
查看>>
【并发编程】实现多线程的几种方式
查看>>
Nginx简介
查看>>
Nginx的Gzip功能
查看>>
基于.Net Core 5.0 Worker Service 的 Quart 服务
查看>>
Azure Storage 系列(四)在.Net 上使用Table Storage
查看>>
我成为 Microsoft Azure MVP 啦!(ps:不是美国职业篮球)
查看>>
异步编程基础
查看>>
[模板] 带修莫队
查看>>
* 二维数组的使用
查看>>
a instanceof A:判断对象a是否是类A的实例。如果是,返回true;如果不是,返回false
查看>>
abstract关键字的使用
查看>>
创建线程的方式四:使用线程池
查看>>
算法题:获取一个字符串在另一个字符串中出现的次数
查看>>
算法题:获取两个字符串中的最大相同子串
查看>>
Calendar日历类(抽象类)的使用
查看>>
Asp.Net Core&Jenkins持续交付到Windows Server
查看>>
自我总结和学习表单提交的几种方式 (一)
查看>>