简体   繁体   English

使用递归搜索字符串以查找另一个字符串

[英]Search string to find another string using recursion

I'm working on the following simple program: 我正在研究以下简单程序:

/**
    Recursively searches a string to find a second string.
    @param s the string to search through.
    @param t the string to search for
    @return true if t is found in s.

    Tests whether the string t is contained in a string s.
    For instance, calling the function like this:
        bool b = find("Mississippi", "sip");
    returns true, since "sip" is contained in "Mississippi".
    You must write this as a recursive function, not by just
    calling the string::find() function, or by using a loop.

*/

bool find(const string& s, const string& t)

{
    string temp = s;    
    if(temp.size() < t.size())    
        return false;   
    temp.erase(0, 1);     
    find(temp, t);    
}

Here's my testing output: 这是我的测试输出:

Checking function: Checking the find recursive function. -------------

   + find("Mississipi", "ipi")
   X find("Mississipi", "ipx") should be false, but is true.
   + find("Sommertown", "Som")
   + find("Sommertown", "Sommertowne")
   + find("Somewhere in the middle", "in")

----------------------------------------------------------------------
  Tests passing 4/5 (80%).

I've written it about 4 other ways, all similar...including one with find(temp.substr(1),t) instead of the temp.erase. 我已经写了大约4种其他方式,所有方式都类似...包括一种使用find(temp.substr(1),t)而不是temp.erase的方式。

Would anyone mind pointing me in the right direction? 有人介意将我指向正确的方向吗? I know it's a simple mistake but I'm not seeing it! 我知道这是一个简单的错误,但我没有看到!

Thank you! 谢谢!

There are two problems with your solution: 您的解决方案有两个问题:

  1. Not all control paths in find explicitly return a value 并非find所有控制路径都显式返回值
  2. t is never compared to s in any way, so find isn't actually checking for t - it's simply getting erased, one character at a time. t绝不会与s进行任何比较,因此find实际上并不是在检查t它只是被删除,一次删除一个字符。

A better way to write your function might be something like this: 编写函数的更好方法可能是这样的:

bool find(const string& s, const string& t)
{
    string temp = s;

    // Boundary condition which should return true
    if (temp.substr(0, t.size()) == t)
        return true;

    // Boundary condition which should return false
    if (temp.size() < t.size())
        return false;

    return find(temp.substr(1), t); // Recursive call
}

^^ Note that in this function, all possible control paths return a value, and t will always be compared to s . ^^请注意,在此函数中,所有可能的控制路径都返回一个值,并且t将始终与s进行比较。

Also note that I said that in your solution, not all control paths were explicitly returning a value. 还要注意,我说过在您的解决方案中,并非所有控制路径都明确地返回一个值。 A C++ function will return a value even if you don't have a return statement, but as @Johan pointed out, it's the last thing evaluated, not true. 即使您没有return语句,C ++函数也将返回值,但是正如@Johan指出的那样,这是最后评估的结果, 而不是正确的。 For instance, I coded up your solution and received the response "224" from find , whereas if find returned true, I would have received the response "1" - or true. 例如,我对您的解决方案进行了编码,并从find收到响应“ 224”,而如果find返回true,则我将收到响应“ 1”-或true。

Your function will always return false. 您的函数将始终返回false。 What it does is it removes one character of string s until s.size() < t.size(). 它的作用是删除字符串s的一个字符,直到s.size()<t.size()。

I would write the function the following way.:) 我将通过以下方式编写该函数:)

bool find( const std::string &s, const std::string &t )
{
    return ( s.size() < t.size() ? 
             false : 
             ( s.compare( 0, t.size(), t ) == 0 ? 
               true :
               find( std::string( s, 1 ), t ) ) );
}

I figured it out guys: 我弄清楚了伙计们:

bool find(const string& s, const string& t)
{
    if(s.length() < t.length()) 
        return false;
    else if(s.substr(0, t.size()) == t)
        return true;

    return find(s.substr(1),t);
}

TESTING H37, loginID
----------------------------------------------------------------------
Checking function: Checking the find recursive function. -------------------
   + find("Mississipi", "ipi")
   + find("Mississipi", "ipx")
   + find("Sommertown", "Som")
   + find("Sommertown", "Sommertowne")
   + find("Somewhere in the middle", "in")
----------------------------------------------------------------------
  Tests passing 5/5 (100%).

Press any key to continue . . .

I appreciate all your help! 感谢您的帮助! This site is a godsend for a new programmer! 这个网站是新程序员的天赐之物!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM