一道C++练习题,替换一个字符串里所有实例
做了一道C++练习题,替换一个字符串里面的所有实例。
#include <iostream>
#include <string>
using namespace std; const int NOT_FOUND = -1;
int strFind(string str, string strSub);
string strRepIdx(string strDes, int idxStart, int idxEnd, string strRepDes);
string strRep(string strDes, string strRepSrc, string strRepDes); // Replace all instances of strRepSrc in strDes with strRepDes and return the string replaced;
string strRep(string strDes, string strRepSrc, string strRepDes)
{
int nIdxFound = strFind(strDes, strRepSrc);
if (NOT_FOUND == nIdxFound)
{
return strDes;
}
else
{
string strLeft = strDes.substr(nIdxFound+ strRepSrc.length(),strDes.length()-nIdxFound);
string strTemp = strRepIdx(strDes, nIdxFound, nIdxFound + strRepSrc.length()-1, strRepDes);
return strTemp.substr(0, nIdxFound + strRepDes.length()) + strRep(strLeft,strRepSrc,strRepDes); }
return strDes;
} // find index of substring strSub in string str;
// return:
// -1,for not found
// 0 or positive int, index of substring
int strFind(string str, string strSub)
{
int strLen = str.length();
int subStrLen = strSub.length();
if (strLen < subStrLen)
{
return NOT_FOUND;
}
else
{
for (int i = 0; i < strLen - subStrLen+1; i++)
{
if (str.substr(i, subStrLen) == strSub)
{
return i;
}
}
}
return NOT_FOUND;
} string strRepIdx(string strDes, int idxStart, int idxEnd, string strRepDes)
{
strDes = strDes.substr(0, idxStart) + strRepDes + strDes.substr(idxEnd+1, strDes.length());
return strDes;
} int main()
{
string str0 = "Hello World";
string subStr1 = "World";
string subStr2 = "l";
string subStrDes = "ii"; cout << "Origin String is " << str0 << endl;
cout << "Replace " << subStr1 << " with " << subStrDes << " is " << strRep(str0, subStr1, subStrDes) << endl;
string strRep2 = strRep(str0, subStr2, subStrDes);
cout << "Replace " << subStr2 << " with " << subStrDes << " is " << strRep2 << endl; return 0;
}
感觉基础的编码水平还基本停留在大一学C语言时候……┗|`O′|┛ 。
一道C++练习题,替换一个字符串里所有实例的更多相关文章
- 【JS新手教程】replace替换一个字符串中所有的某单词
JS中的replace方法可以替换一个字符串中的单词.语句的格式是: 需要改的字符串.replace(字符串或正则表达式,替换成的字符串) 如果第一个参数用字符串,默认是找到该字符串中的第一个匹配的字 ...
- awk如何替换一个字符串的第n个字符?
方法一: echo "abcdefg" | awk 'BEGIN{FS=OFS=""}$4="h"' // ""可 ...
- python-又来练习题--输出一个字符串中最长的子字符串及其长度
一.有个字符串 str= '$sd1#111$svda123!!!221&eSSDSyyyyyyDG^svda121^svda124^1111111111111' 包含特殊字符.数字和字母,输 ...
- java提取出一个字符串里面的Double类型数字
String str="hh\n1\n22\n798.809\n0.89\n"; String regex="\\d+(?:\\.\\d+)?" ...
- JavaScript 中 replace方法 替换所有字符串
需要替换一个字符串中所有的某个字符串 java中使用replaceAll()方法就可以了.但是JavaScript中没有replaceAll方法 但是可以通过以下方法实现: /** * 空格替换为下划 ...
- 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”
//字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...
- 【算法训练营day8】LeetCode344. 反转字符串 LeetCode541. 反转字符串II 剑指Offer05. 替换空格 LeetCode151. 翻转字符串里的单词 剑指Offer58-II. 左旋转字符串
[算法训练营day8]LeetCode344. 反转字符串 LeetCode541. 反转字符串II 剑指Offer05. 替换空格 LeetCode151. 翻转字符串里的单词 剑指Offer58- ...
- 剑指offer 1,输入一个字符串,将字符串的空格替换成%20
剑指offer 1,输入一个字符串,将字符串的空格替换成%20 function replaceSpace(str){ return str.replace(/\s/g,"% ...
- 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动操作使得start可以转换成end时, 返回True。
在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作.一次移动操作指用一个"LX"替换一个"XL ...
随机推荐
- PAT 1026 Table Tennis (30)
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...
- plsql连接其他服务器的oracle
plsql除了连接本地的oracle还需要连接其他服务器上的oracle时 1.下载安装oracleClient:2.在oracleClient安装目录下:例如:D:/instantclient_11 ...
- 查看嵌入式设备的CPU频率
对于有多个核心的CPU,查看CPU 频率的方法是: cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 上面的这个是查看核心0的cpu的 ...
- 修改NavigationBar的分根线颜色
[self.navigationController.navigationBar setShadowImage:[Static ColorToImage:[Static colorWithHexStr ...
- netstat -tulpn
[root@d java]# netstat -tulpnActive Internet connections (only servers)Proto Recv-Q Send-Q Local Add ...
- 剑指Offer——二叉树中和为某一值的路径
题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 分析: 先序遍历二叉树,找到二叉树中结点值的和 ...
- 棋盘格 测量 相机近似精度 (像素精度&物理精度)
像素精度计算 像素精度——一像素对应多少毫米——距离不同像素精度也不同 将棋盘格与相机CCD平面大致平行摆放,通过[每个点处的近似像素精度=相邻两个角点之间的实际距离(棋盘格尺寸已知)/ 棋盘格上检出 ...
- django将数据库中数据直接传到html
1.当然,前提是建立和配置好django数据库啦~ 2.在python后台函数中引入需要的表 #要读取home这个APP下的models.py文件,引入其中的Student_message_unedi ...
- DRF的版本、认证、权限
DRF的版本 版本控制是做什么用的, 我们为什么要用 首先我们要知道我们的版本是干嘛用的呢~~大家都知道我们开发项目是有多个版本的~~ 当我们项目越来越更新~版本就越来越多~~我们不可能新的版本出了~ ...
- OpenCV膨胀与腐蚀
膨胀与腐蚀 本篇博客主要介绍使用OpenCV中的函数接口实现对一个图片的腐蚀或者膨胀,听起来有点像是对图像进行放大和缩小的意思,如果你也是这样认为,那我只能说你跟我一样肤浅!!在OpenCV中几乎所有 ...