- /*
- Copyright (c)2016,烟台大学计算机与控制工程学院
- All rights reserved.
- 文件名称:项目7.cbp
- 作 者:泮春宇
- 完成日期:2016年11月3日
- 版 本 号: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);
- 输入描述:无
- 程序输出:测试数据
- */
头文件sqString.h代码详见顺序串算法库。
(1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符:
void Trans(SqString *&s, char c1, char c2);
代码:
- #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) //= 为赋值,if中==时判断两边是否相等
- s.data[i]=c2;
- }
- int main()
- {
- SqString s;
- StrAssign(s, "messages");
- Trans(s, 'e', 'a');
- DispStr(s);
- return 0;
- }
- <span style="color: rgb(85, 85, 85); font-family: "microsoft yahei"; font-size: 18px;">运行结果:</span>
- <span style="color: rgb(85, 85, 85); font-family: "microsoft yahei"; font-size: 18px;"><img src="https://img-blog.csdn.net/20161023173337083?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
- </span>
- (2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。
- void Invert(SqString &s) ;
- 代码:
- #include <stdio.h>
- #include "sqString.h"
- //将字符串中的第一个元素与最后一个元素进行交换,
- //第二个元素与倒数第二个元素进行交换,以此类推,将所有的字符进行交换,最后将字符串反序。
- 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;
- }
- }
- int main()
- {
- SqString s;
- StrAssign(s, "abcdefg");
- Invert(s);
- DispStr(s);
- return 0;
- }
运行结果:
(3)从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag。
void DellChar(SqString &s, char c) ;
代码:
- #include <stdio.h>
- #include "sqString.h"
- //从头到尾扫描s串,对于其值为c的元素采用移动的方式进行删除。
- void DellChar(SqString &s, char c)
- {
- int i=0,k=0;
- while(i<s.length)
- {
- if(s.data[i]==c)//k记录值等于c的字符个数
- k++;
- else
- s.data[i-k]=s.data[i];
- i++;
- }
- s.length-=k;
- }
- int main()
- {
- SqString s;
- StrAssign(s, "message");
- DellChar(s, 'e');
- DispStr(s);
- return 0;
- }
运行结果:
(4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。
所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。
SqString CommChar(SqString s1,SqString s2);
代码:
- #include <stdio.h>
- #include "sqString.h"
- //对于s1中的每一个字符,查看在s2中是否出现,如果出现,则加到结果字符串中。
- SqString CommChar(SqString s1,SqString s2)
- {
- SqString str;
- int i,j;
- int k=0;
- for(i=0;i<s1.length;i++)
- {
- for(j=0;j<s2.length;j++)
- {
- if(s1.data[i]==s2.data[j])
- {
- str.data[k]=s1.data[i];
- k++;
- }
- }
- }
- str.length=k;
- return str;
- }
- int main()
- {
- SqString s1, s2, s;
- StrAssign(s1, "message");
- StrAssign(s2, "agent");
- s = CommChar(s1, s2);
- DispStr(s);
- return 0;
- }