问题:
要求编写一个删除字符串"abcdeabdbcdaaabdecdf"中所有"abd"的程序。
算法分析:
- 创建两个字符串,使用gets();
- 使用删除字符串的基本操作:DeleteString(SeqString *S, int pos, int len )
- 首先找到要删除的起始字符位置,采用遍历的方式,如果第一个字符相同,则比较第二个,依次进行下去
代码:
顺序串头文件:“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);
}