第八周【串】项目3-顺序串算法
来源:互联网 发布:动态桌面壁纸软件下载 编辑:程序博客网 时间:2024/06/01 13:29
头文件:
#ifndef SqString_H_INCLUDED#define SqString_H_INCLUDED#define MaxSize 100 //最多的字符个数typedef struct{ char data[MaxSize]; //定义可容纳MaxSize个字符的空间 int length; //标记当前实际串长} SqString;void StrAssign(SqString &s,char cstr[]); //字符串常量cstr赋给串svoid StrCopy(SqString &s,SqString t); //串t复制给串sbool StrEqual(SqString s,SqString t); //判串相等int StrLength(SqString s); //求串长SqString Concat(SqString s,SqString t); //串连接SqString SubStr(SqString s,int i,int j); //求子串SqString InsStr(SqString s1,int i,SqString s2); //串插入SqString DelStr(SqString s,int i,int j) ; //串删去SqString RepStr(SqString s,int i,int j,SqString t); //串替换void DispStr(SqString s); //输出串#endif // SqString_H_INCLUDED(1)试编写算法实现将字符串S中所有值为c1的字符换成值为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) s.data[i]=c2;}int main(){ SqString s; StrAssign(s, "messages"); Trans(s, 'e', 'a'); DispStr(s); return 0;}(2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA:
#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:
#include <stdio.h>#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;}(4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。
#include <stdio.h>#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;}
阅读全文
0 0
- 第八周 项目3 顺序串算法
- 第八周--项目3顺序串算法
- 第八周 项目3 顺序串算法
- 第八周项目3 - 顺序串算法
- 第八周项目3顺序串算法
- 第八周-【项目3-顺序串算法】
- 第八周项目3--顺序串算法
- 第八周项目3-顺序串算法
- 第八周项目3-顺序串算法
- (第八周项目3)顺序串算法
- 第八周项目3-顺序串算法
- 第八周项目3--顺序串算法
- 第八周--项目3-顺序串算法
- 第八周项目3-顺序串算法
- 第八周项目3-顺序串算法
- 第八周项目3-顺序串算法
- 第八周项目3-顺序串算法
- 【第八周项目3-顺序串算法】
- Gson原理
- 第八周项目3- 顺序串算法
- 从数据库取得数据
- python基础实例4:天数问题
- 第八周项目一C/C++数据结构之自建算法库——顺序串
- 第八周【串】项目3-顺序串算法
- 踩坑:App上传GooglePlay后Google登录和FaceBook登录异常
- 微信小程序设计weui框架
- BigDecimal.setScale 处理java小数点
- 搭建nginx服务与并支持域名绑定与https
- 第五周 项目1
- HTML5 自动聚焦autofocus属性
- springmvc源码分析----入门看springmvc的加载过程
- 第1086期AI100_机器学习日报(2017-09-08)