c++字符串操作的实现源代码

上传人:飞*** 文档编号:47806587 上传时间:2018-07-05 格式:PDF 页数:12 大小:7.36KB
返回 下载 相关 举报
c++字符串操作的实现源代码_第1页
第1页 / 共12页
c++字符串操作的实现源代码_第2页
第2页 / 共12页
c++字符串操作的实现源代码_第3页
第3页 / 共12页
c++字符串操作的实现源代码_第4页
第4页 / 共12页
c++字符串操作的实现源代码_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《c++字符串操作的实现源代码》由会员分享,可在线阅读,更多相关《c++字符串操作的实现源代码(12页珍藏版)》请在金锄头文库上搜索。

1、Teststring.h: constintmaxsize=100; classmystring public: char chmaxsize;/字符数组,用来存储字符串intcurlen;/ 当前串的长度void show();/ 显示当前串intgetlen();/ 获取当前串的大小void insert(mystring/ 在 s 串中第 i 个位置插入 T字符串!void initmystring();/ 初始化字符串void deletestring (mystring/ 从 s串中的第 i 个位置,删除后面 j 个字符串!void repalacemystring(mystring

2、/ 从 s 中第 i个位置插入 T中的前 j 个字符串!mystring substring(mystring/ 求 s中从第 i 开始, j 个字符的子串!friend mystring operator +(mystring s1,mystring s2 );/ 友元函数重载+运算符实现两个字符串的相加操作!int index(mystrings,mystring T);/求串 T在串 s中出现的首地址!mystring(); ; Teststring.cpp: #include “mystring.h“ #include using namespace std; voidmystring

3、:show() for(inti=0;i=maxsize) cout=i-1;j-) s.chj+T.curlen=s.chj; for(int k=0;k,作为输入时,把 n 给过滤掉了,无法判断哪个时候结束!break; i+; / 永真循环,用于给数组赋值,输入enter 键表示结束!curlen=i;/curlen 取 i 表示字符串只取 i-1 个字符,即把作为末尾的 n去掉! voidmystring:deletestring(mystring else for(int k=i+j-1;ks.curlen|jT.curlen) couts.curlen-i) s.curlen=i+

4、T.curlen-1; elses.curlen=s.curlen; mystringmystring:substring(mystring if(is.curlen|js.curlen-i+1) cout=T.curlen-1) return (i-T.curlen); else return -1; mystring operator +(mystring s1, mystring s2) mystring a;/定义一个 mystring 的对象 a if(s1.curlen+s2.curlenmaxsize) cout #include“mystring.h“ using namesp

5、ace std; void main() mystring s,s1,s6,s_1,s_2,s_3,s_4,s1_1; intlen; couta; switch(a) case 1:int i; couti; s.insert(s,s1,i); coutkj; s.deletestring(s_1,k,j); couter; s.repalacemystring(s_2,s1,e,r); coutxy; s.substring(s_3,x,y); cout“求子串的结果为s5:“endl; s.substring(s_3,x,y).show(); break; case 5:cout“ 串 s1 和 s2链接起来起来的结果得到串s6:“endl; s6=s1+s; s6.show(); break; case 6:cout“s1在 s中的位置为:若输出为 -1,表示 s1不是 s 的子串“endl; couts.index(s_4,s1_1)endl; break; default :cout“命令输入有误 ,请重新输入 “endl;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号