问题及代码:
/*
* Copyright(c) 2017,烟台大学计算机学院
* All rights reserved.
* 文件名称:cpp1.
* 作 者:薛瑞琪
* 完成日期:2017 年 10 月 19 日
* 版 本 号:v1.0
*
* 问题描述: 采用顺序存储方式存储串,实现下列算法并测试:
(1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符: void Trans(SqString *&s, char c1, char c2);
(2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。 void Invert(SqString &s)
(3)从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag。 void DellChar(SqString &s, char c)
(4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。 SqString CommChar(SqString s1,SqString s2);
* 输入描述:无需输入
* 程序输出:实现各种算法的函数的测试结果
*/
#include <stdio.h>
#include "sqString.h"
void Trans(SqString &s, char c1, char c2)
{
int i;
for (i=0; i<s.length; i++)
if (s.data[i]==c1)
s.data[i]=c2;
}
void Invert(SqString &s)
{
int i;
char temp;
for (i=0; i<s.length/2; i++)
{
temp=s.data[i];
s.data[i]=s.data[s.length-i-1];
s.data[s.length-i-1]=temp;
}
}
void DellChar(SqString &s, char c)
{
int k=0,i=0;
while(i<s.length)
{
if(s.data[i]==