繁体   English   中英

在 C++ 中递归地搜索字符数组 substring 中字符数组 substring 的第一次出现

[英]Searching for the first occurrence of a char array substring inside a char array substring recursively in C++

我正在尝试递归搜索字符数组以查找由另一个字符数组组成的序列的第一次出现。 我不允许使用字符串、预定义的字符串函数或循环。 The character arrays that I'm searching in are names used in the program, and this function is used in another function that asks the user for an inputted substring, and then it searches the 5 names in the program for any that have an exact match substring 输入。 我无法在此处发布代码的 rest。 目前,我的程序总是告诉我,无论我输入什么,都找不到名字。

bool IsSubString(char name[], char search_str[], int i, int found_flag, int idx)
{
if (name[i] != '\0' && search_str[idx] != '\0')
    {
        if (search_str[idx] == name[i])
        {
            if (idx == 0)
            {
                found_flag = 1;
            }
            else
            {
                found_flag = found_flag * 1;
            }
            idx++;
        }
        else
        {
            found_flag = 0;
            idx = 0;
        }
        i++;
        IsSubString(name, search_str, i, found_flag, idx);
    }
    

    if (search_str[idx] == '\0')
        return found_flag;
    else
        return false;
}

我发现很难准确理解您的要求,但据我了解,这里有两种解决方案,

如果要检查 name[] == search_str[]:

bool IsEqual(char name[], char search_str[], int i)
{
    if (name[i] == '\0' && search_str[i] == '\0')
        return true;
    if (search_str[i] != name[i])
        return false;

    return IsEqual(name, search_str, ++i) 
}

如果要获取给定字符串中第一次出现 substring 的索引:

/*
    str[] - The string to search in.
    search[] - The substring to search for.
    
    Returns -1 if substring not found.
*/
int getIndex(char str[], char search[], int index, int i,int j)
{
    if( search[j] == '\0' )
        return index;       // Substring found.
    if( str[i] == '\0' )
        return -1; // Search string ended but substring not found.

    if( str[i] == search[j] )
    {
        if( j == 0 ) index = i; // Sets index equal to the index of first charector of search in str 
        j++; ​
    ​}
    else 
    {
        index = -1; // Sets index back to -1 due to missmatched charector.
        j = 0;
    }
    
    return getIndex(str[], search[], index, ++i, j) // Continue search.
}

要使用 function,

getIndex(char_array_to_search, search_string, -1, 0 , 0);

例如,

printf("%d",getIndex("the main string", "main", -1,0,0)); // prints 4
printf("%d",getIndex("one and one", "one", -1,0,0)); // prints 0
printf("%d",getIndex("one and one", "one", -1,1,0)); // prints 8
printf("%d",getIndex("nospace", "nos", -1,0,0)); // prints 0

您必须为 index 传递 -1,为 j 传递 0。 如果您为 i 传递任何大于 0 的数字,则搜索将从该索引开始。

如果您只想知道 substring 是否存在,

bool found = getIndex(str, search, -1, 0,0) != -1;

只是为了好玩,这里有一个压缩得多的版本,也许是为了给你的老师留下深刻印象:

int getIndex(char str[], char search[], int index, int i,int j)
{
    if( search[j] == '\0' )
        return index;       // Substring found.
    if( str[i] == '\0' )
        return -1; // Search string ended but substring not found.

    index =  (j==0) * ((str[i] == search[j]) * (i + 1) - 1)  + (j!=0) * index;
    j += (str[i] == search[j]) - (str[i] != search[j])*j;
    
    return getIndex(str[], search[], index, ++i, j) // Continue search.
}

暂无
暂无

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

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