索引递归

DEnumber50

程序员大家好,我对C ++和其他所有新手都有一个我不了解的递归问题。因此,为了完成本练习,我需要:1.询问用户一个字符串2.询问用户一个字符串以在输入的第一个字符串中进行搜索。3.报告并找到该字符串的索引(如果找到)。例如,用户输入字符串“ Search me”,要搜索的字符串是“ me”,索引将返回“ 7”。在这一点上,我将举手寻求有关如何完成它的帮助,我们将不胜感激。到目前为止,这是我的代码。for循环中的内容并不是完整的想法,仅是仅供参考。

int index_of(string stringToSearchIn, string stringToSearchFor, int index)
{

if (stringToSearchIn.length() < stringToSearchFor.length())
{
    cout << "Your string cannot be found." << endl;
    system("PAUSE");
    return OUTLIER;
}
else
{
    bool found = true;
    for (int i = ZERO; i < stringToSearchFor.length; i++)
    {
        found = found && stringToSearchIn(i) == stringToSearchFor(i);
    }
    return index;
}

return index_of(stringToSearchIn.substr(INCREASE), stringToSearchFor, index++);
}

int main()
{
//Initializing values
string userString;
string userSearch;
int userIndex = 0;

//Asking for user input for string
cout << "This program will find the occurence of one string inside of another." << endl;
cout << "Enter the string to be searched: " << userString;
//Getting the string
getline(cin, userString);

//Asking for user input for search input
cout << "Now enter the string you want to search for: " << userSearch;
//Getting the string
getline(cin, userSearch);

//Displaying results
cout << "The index of the substring is = " << index_of(userString, userSearch, userIndex);

//Keeping console window open until key press
system("PAUSE");
return ZERO;
}
来自莫斯科的弗拉德

抓住。:)

#include <iostream>

size_t index_of( const char *s, const char *t )
{
    const char *p = s;
    const char *q = t;

    while ( *q && *p == *q ) ++p, ++q;

    if ( *q == '\0' )
    {
        return 0;
    }
    else if ( *p == '\0' )
    {
        return -1;
    }
    else
    {
        size_t n = 1 + index_of( s + 1, t );
        return n == 0 ? -1 : n;
    }
}

int main() 
{
    const char *s = "Search me";
    const char *t = "me";

    size_t n = index_of( s, t );

    if ( n != -1 )
    {
        std::cout << "string " << t << " is found at position " << n << std::endl;
    }
    else
    {
        std::cout << "string " << t << " is not found" << std::endl;
    }

    return 0;
}

输出是

string me is found at position 7

对于其他字符串,我没有测试功能。

对于类型的对象,std::string您可以这样称呼它

size_t n = index_of( s.c_str(), t.c_str() );

否则,如果您要为std :: string类型的对象编写类似的递归函数,则需要添加一个静态局部变量以将当前位置保留在源字符串中,或​​者为索引添加另一个参数或使用成员函数substr 。

另外,我的建议是不要将清单常数ZERO设为零。我认为这是一种不好的编程风格。它只会使读者感到困惑,因为零可以是任何东西,包括一些用户定义的类。

例如(未经测试)

#include <iostream>
#include <string>

std::string::size_type index_of( const std::string &s, const std::string &t )
{
    static std::string::size_type pos;

    if ( s.size() < t.size() ) return std::string::npos;

    if ( s.compare( pos, t.size(), t ) == 0 ) return 0;

    ++pos;

    std::string::size_type n = index_of( s, t );

    --pos;

    return n == std::string::npos ? std::string::npos : n + 1;
}


int main() 
{
    std::string s = "Search me";
    std::string t = "me";

    std::string::size_type n = index_of( s, t );

    if ( n != std::string::npos )
    {
        std::cout << "string " << t << " is found at position " << n << std::endl;
    }
    else
    {
        std::cout << "string " << t << " is not found" << std::endl;
    }


    return 0;
}

至于您的功能实现,则可以例如以下方式查找

#include <iostream>
#include <string>

std::string::size_type index_of( const std::string &stringToSearchIn, 
                                 const std::string &stringToSearchFor, 
                                 std::string::size_type index )
{
    if ( stringToSearchIn.length() < stringToSearchFor.length() + index )
    {
        return std::string::npos;
    }
    else if ( stringToSearchIn.compare( index, 
                                        stringToSearchFor.length(),
                                        stringToSearchFor ) == 0 )
    {
        return index;
    }
    else
    {
        std::string::size_type n = 
            index_of( stringToSearchIn, stringToSearchFor, ++index );
        return n == std::string::npos ? std::string::npos : n;
    }
}

int main() 
{
    std::string s = "Search me";
    std::string t = "me";

    std::string::size_type n = index_of( s, t, 0 );

    if ( n != std::string::npos )
    {
        std::cout << "string " << t << " is found at position " << n << std::endl;
    }
    else
    {
        std::cout << "string " << t << " is not found" << std::endl;
    }

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章