两个字符串的递归比较
Recursive comparison of two strings
函数int compare(...),检查两个字符串是否相等忽略大小写和任何非字母字符,例如"a?...!b" 等同于 "ab"。 Returns 如果相等则为 1,否则为 0。但是,我的代码中有一个错误!
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 == '[=10=]')
return 1;
if(*string1 == *string2)
compare(++string1, ++string2);
}
如果我尝试 运行 此代码,例如:
compare("a !!!b", "a b");
输出结果让我很困惑:
a | b
|
! |
! |
! |
b | b
^@| ^@
| a
^@| ^@
| a
它returns 0(不等于)。一旦到达 b,它就不会停止 运行ning | b,为什么?
除了需要 return
语句之外,您的逻辑还有缺陷。您需要在函数的前面检查两个字符串是否为空并因此相等:
int compare(const char* string1, const char* string2)
{
if(string1 == NULL || string2 == NULL)
return 0;
// This needs to go here
if(*string1 == '[=10=]' && *string2 == '[=10=]') {
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);
}
您可以在这里查看:https://ideone.com/Si78Nz
函数int compare(...),检查两个字符串是否相等忽略大小写和任何非字母字符,例如"a?...!b" 等同于 "ab"。 Returns 如果相等则为 1,否则为 0。但是,我的代码中有一个错误!
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 == '[=10=]')
return 1;
if(*string1 == *string2)
compare(++string1, ++string2);
}
如果我尝试 运行 此代码,例如:
compare("a !!!b", "a b");
输出结果让我很困惑:
a | b
|
! |
! |
! |
b | b
^@| ^@
| a
^@| ^@
| a
它returns 0(不等于)。一旦到达 b,它就不会停止 运行ning | b,为什么?
除了需要 return
语句之外,您的逻辑还有缺陷。您需要在函数的前面检查两个字符串是否为空并因此相等:
int compare(const char* string1, const char* string2)
{
if(string1 == NULL || string2 == NULL)
return 0;
// This needs to go here
if(*string1 == '[=10=]' && *string2 == '[=10=]') {
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);
}
您可以在这里查看:https://ideone.com/Si78Nz