Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 两个字符串的递归比较_C++_Recursion_C Strings - Fatal编程技术网

C++ 两个字符串的递归比较

C++ 两个字符串的递归比较,c++,recursion,c-strings,C++,Recursion,C Strings,函数int compare(…)检查两个字符串是否相等(忽略大小写),以及是否有任何非字母字符,例如“a?…!b”等同于“ab”。如果相等,则返回1,否则返回0。然而,我的代码中有一个bug int compare(const char* string1, const char* string2) { if(string1 == NULL || string2 == NULL) return 0; std::cout << *string1 << "

函数int compare(…)检查两个字符串是否相等(忽略大小写),以及是否有任何非字母字符,例如“a?…!b”等同于“ab”。如果相等,则返回1,否则返回0。然而,我的代码中有一个bug

int compare(const char* string1, const char* string2)
{
  if(string1 == NULL || string2 == NULL)
    return 0;

   std::cout << *string1 << " | " << *string2 << std::endl;
   if((!isalpha(*string1) && *string1 != ' ') && (!isalpha(*string2) && *string2 != ' '))
    {
      compare(++string1,++string2);
    }
   else if(!isalpha(*string1) && *string1 != ' ')
    {
      compare(++string1,string2);
    }
   else if(!isalpha(*string2) && *string2 != ' ')
    {
     compare(string1, ++string2);
    }

  if(tolower(*string1) != tolower(*string2))
    return 0;
  if(*string1 == '\0')
    return 1;
  if(*string1 == *string2)
    compare(++string1, ++string2);
}
输出结果让我很困惑:

a | b
  | 
! | 
! | 
! | 
b | b
^@| ^@
  | a
^@| ^@
  | a

它返回0(不相等)。它不会在到达b | b后停止运行,为什么?

除了需要
return
语句之外,您的逻辑中还有一个缺陷。您需要检查这两个字符串是否为空,从而在函数的前面部分相等:

int compare(const char* string1, const char* string2)
{
    if(string1 == NULL || string2 == NULL)
        return 0;

    // This needs to go here
    if(*string1 == '\0' && *string2 == '\0') {
        return 1;
    }

    std::cout << *string1 << " | " << *string2 << std::endl;
    if((!isalpha(*string1) && *string1 != ' ') && (!isalpha(*string2) && *string2 != ' '))
    {
        return compare(++string1,++string2);
    }
    else if(!isalpha(*string1) && *string1 != ' ')
    {
        return compare(++string1,string2);
    }
    else if(!isalpha(*string2) && *string2 != ' ')
    {
        return compare(string1, ++string2);
    }

    if(tolower(*string1) != tolower(*string2))
        return 0;
    if(*string1 == *string2)
        return compare(++string1, ++string2);
}
int比较(常量字符*string1,常量字符*string2)
{
if(string1==NULL | | string2==NULL)
返回0;
//这个需要放在这里
如果(*string1=='\0'&&&*string2=='\0'){
返回1;
}

std::您是否需要在每次调用
compare
之前放置
return
,否则它将递归调用
compare
,然后继续执行该函数。请注意,当
char
值为负值时,使用
函数和
char
值将导致未定义的行为!您应该ld将
char
值强制转换为
unsigned char
,然后将其从
传递给任何函数。除了@johnnymapp提到的内容外,在函数末尾没有
return
语句:从函数末尾掉落,返回与
void
不同的内容,而没有
return
语句是未定义的行为。当您递归调用“compare()”时,您的代码将丢弃返回值。如果在所有调用
compare
之前都有返回语句,那么如何发生递归?
int compare(const char* string1, const char* string2)
{
    if(string1 == NULL || string2 == NULL)
        return 0;

    // This needs to go here
    if(*string1 == '\0' && *string2 == '\0') {
        return 1;
    }

    std::cout << *string1 << " | " << *string2 << std::endl;
    if((!isalpha(*string1) && *string1 != ' ') && (!isalpha(*string2) && *string2 != ' '))
    {
        return compare(++string1,++string2);
    }
    else if(!isalpha(*string1) && *string1 != ' ')
    {
        return compare(++string1,string2);
    }
    else if(!isalpha(*string2) && *string2 != ' ')
    {
        return compare(string1, ++string2);
    }

    if(tolower(*string1) != tolower(*string2))
        return 0;
    if(*string1 == *string2)
        return compare(++string1, ++string2);
}