C++:递归地用另一个字母替换字符串中一个字母的所有实例
C++: recursively replace all instances of a letter in a string with another letter
我正在复习一些教科书 C++ 问题,其中一个是编写一个函数,该函数递归地 将字符串中某个字母的所有实例替换为另一个字母。我知道这有预先存在的功能,但是因为本章着重于递归,所以这道题坚持认为解决方案必须是递归的。所以我用 C++ 写了所有这些,这很好,但后来我阅读了问题的脚注,它说的是:"for the manipulation of string objects, only the methods at and length (i.e. size) are allowed as well as the operator +"。哇?我只是不明白如果没有 str.substr(pos,len) 你怎么能做到这一点,但如果有人能找到办法,我会很高兴。非常感谢那个特别的人哟。
这是我的仓鼠大脑能想出的最好的代码(也是在开始时注释掉的一个小的迭代替代方案)。
#include <iostream>
#include <string>
using namespace std;
// iterative solution
/* string replace (string in, char from, char to) {
string res;
for (int i{0}; i < in.length(); i++) {
if (in.at(i) == from)
res += to;
else
res += in.at(i);
}
return res;
} */
// recursive solution
string replace (string in, char from, char to) {
if (in.empty())
return "";
char first{in.at(0)};
if (first == from)
return to + replace (in.substr(1), from, to);
else
return in.at(0) + replace (in.substr(1), from, to);
}
int main () {
string in;
char from, to;
cout << "Word: ";
cin >> in;
cout << "from: ";
cin >> from;
cout << "to: ";
cin >> to;
cout << in << " --> " << replace (in, from, to) << '\n';
return 0;
}
只需提供一个跟踪索引的默认参数:
string replace(string in, char from, char to, int i = 0)
{
if (i == in.length())
return in;
if (in.at(i) == from)
in.at(i) = to;
return replace(in, from, to, i + 1);
}
这是 demo。
这只使用了at()
和length()
,甚至没有使用+
。
此外,请避免 using namespace std;
,这是不好的做法。
考虑到脚注
I read the footnote to the question and what it says is: "for the
manipulation of string objects, only the methods at and length (i.e.
size) are allowed as well as the operator +".
函数看起来应该如下所示
std::string & replace( std::string &in, char from, char to, std::string::size_type pos = 0 )
{
if ( pos < in.size() )
{
if ( in.at( pos ) == from )
{
in.at( pos ) = to;
}
replace( in, from, to, pos + 1 );
}
return in;
}
这是一个演示程序
#include <iostream>
#include <string>
std::string & replace( std::string &in, char from, char to, std::string::size_type pos = 0 )
{
if ( pos != in.size() )
{
if ( in.at( pos ) == from )
{
in.at( pos ) = to;
}
replace( in, from, to, pos + 1 );
}
return in;
}
int main()
{
std::string in( "Hell& W&rld!" );
char from = '&';
char to = 'o';
std::cout << in << " --> ";
std::cout << replace( in, from, to ) << '\n';
return 0;
}
它的输出是
Hell& W&rld! --> Hello World!
考虑到 "to replace all instances of a letter in a string with another letter" 意味着必须更改源字符串。这反过来意味着源字符串必须通过引用传递给函数。
我正在复习一些教科书 C++ 问题,其中一个是编写一个函数,该函数递归地 将字符串中某个字母的所有实例替换为另一个字母。我知道这有预先存在的功能,但是因为本章着重于递归,所以这道题坚持认为解决方案必须是递归的。所以我用 C++ 写了所有这些,这很好,但后来我阅读了问题的脚注,它说的是:"for the manipulation of string objects, only the methods at and length (i.e. size) are allowed as well as the operator +"。哇?我只是不明白如果没有 str.substr(pos,len) 你怎么能做到这一点,但如果有人能找到办法,我会很高兴。非常感谢那个特别的人哟。
这是我的仓鼠大脑能想出的最好的代码(也是在开始时注释掉的一个小的迭代替代方案)。
#include <iostream>
#include <string>
using namespace std;
// iterative solution
/* string replace (string in, char from, char to) {
string res;
for (int i{0}; i < in.length(); i++) {
if (in.at(i) == from)
res += to;
else
res += in.at(i);
}
return res;
} */
// recursive solution
string replace (string in, char from, char to) {
if (in.empty())
return "";
char first{in.at(0)};
if (first == from)
return to + replace (in.substr(1), from, to);
else
return in.at(0) + replace (in.substr(1), from, to);
}
int main () {
string in;
char from, to;
cout << "Word: ";
cin >> in;
cout << "from: ";
cin >> from;
cout << "to: ";
cin >> to;
cout << in << " --> " << replace (in, from, to) << '\n';
return 0;
}
只需提供一个跟踪索引的默认参数:
string replace(string in, char from, char to, int i = 0)
{
if (i == in.length())
return in;
if (in.at(i) == from)
in.at(i) = to;
return replace(in, from, to, i + 1);
}
这是 demo。
这只使用了at()
和length()
,甚至没有使用+
。
此外,请避免 using namespace std;
,这是不好的做法。
考虑到脚注
I read the footnote to the question and what it says is: "for the manipulation of string objects, only the methods at and length (i.e. size) are allowed as well as the operator +".
函数看起来应该如下所示
std::string & replace( std::string &in, char from, char to, std::string::size_type pos = 0 )
{
if ( pos < in.size() )
{
if ( in.at( pos ) == from )
{
in.at( pos ) = to;
}
replace( in, from, to, pos + 1 );
}
return in;
}
这是一个演示程序
#include <iostream>
#include <string>
std::string & replace( std::string &in, char from, char to, std::string::size_type pos = 0 )
{
if ( pos != in.size() )
{
if ( in.at( pos ) == from )
{
in.at( pos ) = to;
}
replace( in, from, to, pos + 1 );
}
return in;
}
int main()
{
std::string in( "Hell& W&rld!" );
char from = '&';
char to = 'o';
std::cout << in << " --> ";
std::cout << replace( in, from, to ) << '\n';
return 0;
}
它的输出是
Hell& W&rld! --> Hello World!
考虑到 "to replace all instances of a letter in a string with another letter" 意味着必须更改源字符串。这反过来意味着源字符串必须通过引用传递给函数。