【问题标题】:C++: recursively replace all instances of a letter in a string with another letterC ++:用另一个字母递归替换字符串中字母的所有实例
【发布时间】:2020-08-27 15:16:21
【问题描述】:

我正在研究一些教科书 C++ 问题,其中之一是编写一个函数,该函数递归 将字符串中某个字母的所有实例替换为另一个字母。我知道对此已有预先存在的功能,但是由于本章重点介绍递归,因此该问题坚持认为解决方案必须是递归的。所以我用 c++ 写了所有这些,这很好,但后来我读了问题的脚注,它说的是:“对于字符串对象的操作,只有方法 at长度(即size)以及运算符+”都是允许的。哇?我只是不明白如果没有 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;
}

【问题讨论】:

    标签: c++ string recursion replace function-definition


    【解决方案1】:

    考虑脚注

    我阅读了该问题的脚注,其内容是:“对于 操作字符串对象,只有 at 和 length 的方法(即 size) 以及运算符 +" 都是允许的。

    这个函数看起来应该是这样的

    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!
    

    考虑到“用另一个字母替换字符串中一个字母的所有实例”意味着必须更改源字符串。这反过来意味着源字符串必须通过引用传递给函数。

    【讨论】:

    • 哦,是的,所以无论如何都要使用某种索引,谢谢。哦对了,如果修改后的字符串要在程序后面使用,我应该通过引用传递,好建议!
    【解决方案2】:

    只需提供一个跟踪索引的默认参数:

    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;,这是不好的做法。

    【讨论】:

    • 哦,哇!非常感谢并感谢您的提示,以后我会省略using namespace std;
    • 没问题。如果对您有用,请考虑accepting 的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2023-04-09
    • 2013-09-27
    • 2012-10-10
    • 1970-01-01
    • 2022-07-07
    相关资源
    最近更新 更多