std::string find_replace_str(std::string s,const std::string &oldVal,const std::string &newVal)
{
/*****************************************************************************************
Function: find_replace_str
Description: Finding old string value in giving string s and replace it by new string.
Input: String s is the original string going to be replaced.
String oldVal is the replace target.
String newVal is the final string which replaced the old value.
If string s is "tho tho hello world tho", oldVal is "tho" and newVal is "though",
find_replace_str(s, oldVal, newVal) return "though though hello world though" to
be the return value.
Author: TianYi
Date:2023-03-10
****************************************************************************************/
std::string::iterator it_n = s.begin();
auto oldRange = static_cast<std::string::difference_type>(oldVal.size());
int replace_time = 0; //record the replaced time
while (it_n != s.end()){
if (*it_n == oldVal[0]){
std::string::size_type oldSize = oldVal.size();
std::string::size_type counter = 0;
for (decltype(oldSize) i = 0; i < oldSize; ++i){
if (*(it_n + static_cast<std::string::difference_type>(i)) == oldVal[i])
++counter;
}
// find and insert
if (counter == oldSize){
auto it_range_end = it_n + oldRange;
it_n = s.erase(it_n, it_range_end); // delete old char
//insert new string
auto it_new_re = newVal.crbegin();
while (it_new_re != newVal.crend()){
it_n = s.insert(it_n, *it_new_re);
++it_new_re;
}
//iterator move
it_n += oldRange;
++replace_time;
std::cout << "Replace operator has completed " << replace_time << " times.\n";
}
} else
++it_n; // iterator move
}
return s;
}
#include <string>
#include <list>
#include <iostream>
std::string find_replace_str(std::string s,const std::string &oldVal,const std::string &newVal)
{
/*****************************************************************************************
Function: find_replace_str
Description: Finding old string value in giving string s and replace it by new string.
Input: String s is the original string going to be replaced.
String oldVal is the replace target.
String newVal is the final string which replaced the old value.
If string s is "tho tho hello world tho", oldVal is "tho" and newVal is "though",
find_replace_str(s, oldVal, newVal) return "though though hello world though" to
be the return value.
Author: TianYi
Date:2023-03-10
****************************************************************************************/
std::string::iterator it_n = s.begin();
auto oldRange = static_cast<std::string::difference_type>(oldVal.size());
int replace_time = 0; //record the replaced time
while (it_n != s.end()){
if (*it_n == oldVal[0]){
std::string::size_type oldSize = oldVal.size();
std::string::size_type counter = 0;
for (decltype(oldSize) i = 0; i < oldSize; ++i){
if (*(it_n + static_cast<std::string::difference_type>(i)) == oldVal[i])
++counter;
}
// find and insert
if (counter == oldSize){
auto it_range_end = it_n + oldRange;
it_n = s.erase(it_n, it_range_end); // delete old char
//insert new string
auto it_new_re = newVal.crbegin();
while (it_new_re != newVal.crend()){
it_n = s.insert(it_n, *it_new_re);
++it_new_re;
}
//iterator move
it_n += oldRange;
++replace_time;
std::cout << "Replace operator has completed " << replace_time << " times.\n";
}
} else
++it_n; // iterator move
}
return s;
}
//test
int main()
{
std::string test = "tho tho hello world tho";
std::string re = find_replace_str(test, "tho", "though");
std::cout << re;
return 0;
}