串---删除字符串中的所有子串

问题:

要求编写一个删除字符串"abcdeabdbcdaaabdecdf"中所有"abd"的程序。

算法分析:

  1. 创建两个字符串,使用gets();
  2. 使用删除字符串的基本操作:DeleteString(SeqString *S, int pos, int len )
  3. 首先找到要删除的起始字符位置,采用遍历的方式,如果第一个字符相同,则比较第二个,依次进行下去

代码:

顺序串头文件:“SeqString.h”,串的基本操作

#pragma once
#define MaxLen 80
#include<iostream>
typedef struct
{
	char str[MaxLen];
	int length;
} SeqString;

void StrString(SeqString *s, char cstr[])
/*串的赋值操作*/
{
	int i = 0;
	for (i = 0; cstr[i] != '\0'; i++)
	{
		s->str[i] = cstr[i];
		s->length = i + 1;

	}
}

int StrEmpty(SeqString s)
/*判断是否为空*/
{
	if (s.length == 0)
		return 1;
	else
		return 0;

}

int StrLength(SeqString S)
/*求串的长度*/
{
	return S.length;
}

void StrCopy(SeqString *T, SeqString S)
{
	int i;
	for (i = 0; S.str[i] != '\0'; i++)
	{
		T->str[i] = S.str[i];
		T->length = i + 1;
	}

}

int StrCompare(SeqString S, SeqString T)
/*比较两个串的大小,就是比较每个字符的ASCII值的大小,如果S大于T,返回1,等于返回0, 小于返回-1*/
{
	int i = 0;
	while (S.str[i] != '\0' && T.str[i] != '\0')
	{
		if (S.str[i] != T.str[i])
			return(S.str[i] - T.str[i]);
	}
	return (S.length-T.length);//比较完毕返回长度的差值

}

int StringInsert(SeqString *S, int pos, SeqString T)
/*在串S中的pos位置插入T,有三种情况*/
{
	int i;
	if ((S->length + T.length) <= MaxLen)
		//T能够完全插入进去
	{
		//先将pos往后移动
		for (i = S->length + T.length - 1; i >= pos+T.length-1; i--)
		{
			S->str[i] = S->str[i - T.length];
		}
		//插入
		for (i = pos; i < pos + T.length-1; i++)
		{
			S->str[i] = T.str[i - pos];
		}
		S->length += T.length;
		return 1;
	}

	else if( (pos + T.length <= MaxLen)&&( T.length +S->length > MaxLen) ) // T能完全插入,S要被舍去一部分
	{
		//先移动
		for (i = MaxLen - 1; i >= pos+T.length-1; i++)
		{
			S->str[i] = S->str[i - T.length];
		}
		//插入
		for (i = pos; i < pos + T.length - 1; i++)
		{
			S->str[i] = T.str[i-pos];

		}

		S->length = MaxLen;
		return 0;


	}
	else //S的pos后面部分被全部删除
	{
		for (i = pos; i <= MaxLen - 1; i++)
		{
			S->str[i] = T.str[i - pos];
		}
		return 0;
	}
}


int StrDelete(SeqString *S, int pos, int len)
/*删除pos开始的len个字符*/
{
	int i;
	if (pos < 0 || len<0 || pos + len >S->length)
	{
		std::cout << "参数不合法" << std::endl;

	}
	else //覆盖掉pos开始后的len个字符
	{
		for (i = pos+len ;i< S->length;i++)
		{
			S->str[i-len] = S->str[i];
		}
		S->length -= len;
		return 1;
	}


}

int StrCat(SeqString *S, SeqString T)
/*将T连在S的末端*/
{
	int i;
	if (S->length + T.length <= MaxLen)//如果小于直接连
	{
		for (i = S->length ; i < S->length + T.length; i++)
		{
			S->str[i] = T.str[i - S->length];
		}
		S->length += T.length;
		return 1;
	}
	else //T要部分丢失
	{
		for (i = S->length; i < MaxLen; i++)
		{
			S->str[i] = T.str[i - S->length];
		}
		S->length = MaxLen;
		return 0;
	}
}

void StrClear(SeqString *S)
/*清空*/
{
	S->length = 0;
}

.cpp文件

#include "pch.h"
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include"SeqString.h"

void CreateStr(SeqString *S, char str[])
/*创建字符串*/
{
	strcpy_s(S->str, str);
	S->length = strlen(str);
}


void PrintStr(SeqString S)
/*打印显示字符串*/
{
	int i;
	for (i = 0; i < S.length; i++)
	{
		printf("%c", S.str[i]);
	}
	printf("\n");
}


int Index(SeqString S1, SeqString S2)
//找到一个位置
{
	int i = 0;
	int k = 0;
	int m=0;
	while (i < S1.length)
	{
		/*if (S1.str[i] == S2.str[k])
		{
			m = i + 1;
			k++;
			if (S1.str[m] == S2.str[k])
			{
				m = m + 1;
				k++;
				if (S1.str[m] == S2.str[k])
				{
					return i;
				}
			}
		}
		else
		{
			i++;
			k = 0;
		}*/
		m = i;
		while (S1.str[m] == S2.str[k] && k < S2.length)
		{
			m++;
			k++;
		}
		if (k == S2.length )
			return i;
		else
		{
			i++;
			k = 0;
		}

	}
	return -1;
	
}

int main()
{
	SeqString S1, S2;
	char str[MaxLen];
	std::cout << "输入字符串" << std::endl;
	gets_s(str);
	CreateStr(&S1, str);
	PrintStr(S1);
	std::cout << "子字符串" << std::endl;
	gets_s(str);
	CreateStr(&S2, str);
	PrintStr(S2);
	while (Index(S1,S2)>=0)
	{
		StrDelete(&S1, Index(S1, S2), S2.length);
	}
	PrintStr(S1);


}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XIE_QAID

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值