/* Copyright (c)2015,烟台大学计算机与控制工程学院 All rights reserved. 文件名称:项目3.cbp 作 者:刘晨筱 完成日期:2015年11月30日 版 本 号:v1.0 问题描述:采用顺序存储方式存储串,实现下列算法并测试。 输入描述:无 程序输出:测试数据 */头文件代码详情见【 顺序串算法库】
(1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符: void Trans(SqString *&s, char c1, char c2);
代码:
#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; } int main() { SqString s; StrAssign(s, "messages"); Trans(s, 'e', 'a'); DispStr(s); return 0; }
运行结果:
<img src="http://img.blog.csdn.net/20151130194919272" alt="" />
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.454545021057129px; line-height: 35px;">(2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。 void Invert(SqString &s) </p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.454545021057129px; line-height: 35px;">代码:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.454545021057129px; line-height: 35px;"><pre name="code" class="cpp">#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; } 运行结果:
<img src="http://img.blog.csdn.net/20151130194855390" alt="" />
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.454545021057129px; line-height: 35px;">(3)从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag。 void DellChar(SqString &s, char c) </p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.454545021057129px; line-height: 35px;">代码:</p><pre name="code" class="cpp">#include "sqstring.h" void DellChar(SqString &s, char c) { int k=0, i=0; //k记录值等于c的字符个数 while(i<s.length) { if(s.data[i]==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; }
<img src="http://img.blog.csdn.net/20151130194722930" alt="" />
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.454545021057129px; line-height: 35px;">(4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。 SqString CommChar(SqString s1,SqString s2);</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.454545021057129px; line-height: 35px;">代码:</p><pre name="code" class="cpp">#include "sqstring.h" SqString CommChar(SqString s1,SqString s2) { SqString s3; int i,j,k=0; for (i=0; i<s1.length; i++) { for (j=0; j<s2.length; j++) if (s2.data[j]==s1.data[i]) break; if (j<s2.length) //s1.data[i]是公共字符 { s3.data[k]=s1.data[i]; k++; } } s3.length=k; return s3; } int main() { SqString s1, s2, s; StrAssign(s1, "message"); StrAssign(s2, "agent"); s = CommChar(s1, s2); DispStr(s); return 0; } 运行结果:
<img src="http://img.blog.csdn.net/20151130194659768" alt="" />
<img src="file:///c:/users/viking/appdata/roaming/360se6/User Data/temp/20151022122735862.png" alt="" />
<img src="file:///c:/users/viking/appdata/roaming/360se6/User Data/temp/20151022122735862.png" alt="" />