• 顺序串的基本操作


    #include<stdio.h>
    #define MaxSize 255
    typedef struct
    {
    	char ch[MaxSize];
    	int length;
    }SString;
    void InitStr(SString &S)	
    {
    	S.ch[0]='¥';
    	S.length=0;
    }
    void StrCreate(SString &S,char a[])		//1.赋值操作
    {
    	int i=0,j=1;
    	while(a[i]!='')
    	{
    		S.ch[j++]=a[i++];
    	}
    	S.length=i;
    }
    void StrCopy(SString &S,SString T)	//2.复制操作
    {
    	for(int i=1;i<T.length;i++)
    		S.ch[i]=T.ch[i];
    	S.length=T.length;
    }
    bool StrEmpty(SString S)	//3.判空操作
    {
    	return S.length==0;
    }
    int StrLen(SString S)	//4.求串长
    {
    	return S.length;
    }
    void ClearStr(SString &S)	//5.清空操作
    {
    	S.length=0;
    }
    void Concat(SString &T,SString S1,SString S2)	//6.串链接
    {
    	int i;
    	for(i=1;i<=S1.length;i++)	//将串S1复制到T
    		T.ch[i]=S1.ch[i];
    	for(i=1;i<=S2.length;i++)	//将S2复制到T尾部
    		T.ch[S1.length+i]=S2.ch[i];
    	T.length=S1.length+S2.length;
    }
    bool SubString(SString S,SString &Sub,int front,int len)	//7.求子串
    {
    	if(S.length<front+len-1)	//判断子串是否越界
    		return false;
    	for(int i=1;i<=len;i++)
    		Sub.ch[i]=S.ch[front++];
    	Sub.length=len;
    	return true;
    }
    int CompareStr(SString S,SString T)	//8.比较操作
    {
    	int i=1;
    	while(i<S.length && i<T.length && S.ch[i]==T.ch[i])
    		i++;
    	if(S.ch[i]!=T.ch[i])	
    		return S.ch[i]-T.ch[i];	//S>T 返回值>0;S<T 返回值<0;S=T 返回值=0
    	return S.length-T.length;	//扫描过的字符一致,长串大
    }
    int Index(SString S,SString T)	//9.定位操作
    {
    	int i=1,m=S.length,n=T.length;
    	SString Sub;
    	while(i<m-n+1)		//长度为T.length的子串个数
    	{
    		SubString(S,Sub,i,T.length);	//依次求子串
    		if(CompareStr(Sub,T)==0)	//判断两串是否相等
    			return i;	//返回位置i
    		i++;
    	}
    	return 0;
    }
    void PrintStr(SString S)	//10.输出操作
    {
    	printf("the Strings are:");
    	for(int i=1;i<=S.length;i++)
    		printf("%c	",S.ch[i]);
    	printf("
    ");
    }
    int main()
    {
    	SString S;
    	InitStr(S);
    	char a[]="abcdefg";
    	printf("The Strings is %s
    ",StrEmpty(S)?"空":"非空");
    	StrCreate(S,a);
    	printf("The Strings is %s
    ",StrEmpty(S)?"空":"非空");
    	PrintStr(S);
    	printf("The S's length is %d
    ",S.length);
    	SString Sub;
    	SubString(S,Sub,2,3);
    	PrintStr(Sub);
    	SString T;
    	Concat(T,S,Sub);
    	PrintStr(T);
    	printf("The T's length is %d
    ",T.length);
    	int i=CompareStr(S,T);
    	if(i==1)
    		printf("The max is %c
    ",'S');
    	else if(i==0)
    		printf("The max is S and T
    ");
    	else 
    		printf("The max is %c
    ",'T');
    	printf("The Sub is Num %d in S
    ",Index(S,Sub));
    	char b[]="acdbg";
    	SString R;
    	StrCreate(R,b);
    	printf("The Sub is Num %d in S
    ",Index(S,R));
    	ClearStr(T);
    	printf("The Strings is %s
    ",StrEmpty(T)?"空":"非空");
    	return 0;
    }
    

      

  • 相关阅读:
    22算法中的O(1), O(n),O(logn), O(nlogn)是什么意思?
    212数据结构队列/优先队列
    02前端项目代码检查规范
    16对element源码进行修改实现上传列表增加上传文件大小显示
    182 尾调用/尾递归/尾递归优化/柯里化函数/蹦床函数
    2.formdata数据格式和json格式的区别
    18柯里化函数/偏函数
    MD5文件验证
    tomcat 部署运行静态html文件
    矩阵快速幂及其简单应用
  • 原文地址:https://www.cnblogs.com/-slz-2/p/13289924.html
Copyright © 2020-2023  润新知