问题
Is it even possible to define a recursive method for palindrome checkup with the following argument list?
int testPalindromeRecursive(char* str, int len) { ... }
Note: no external sub-functions or global variables have to be used
I think this is impossible, because you have to remember the last (front) index position somehow.
回答1:
Yes, it is completely possible - as several people have mentioned.
Base Cases:
- If len <= 1, return True
- If str[0] != str[len-1] return False
Else: recurse with (str+1, len -2)
回答2:
1) A string with no characters or just a single character is a palindrome
2) if the first and last characters of a string with 2 or more characters are equal, and the substring excluding the terminal characters is a palindrome, the whole string is a palindrone.
回答3:
As for me then I would declare the function like
int testPalindromeRecursive( const char *s, size_t n );
In this case the function would contain only one return statement
int testPalindromeRecursive( const char *s, size_t n )
{
return ( n < 2 ) ||
( s[0] == s[n-1] && testPalindromeRecursive( s + 1, n - 2 ) );
}
Nevertheless the function can be wriiten the following way as it is shown in the demonstrative program below
#include <stdio.h>
int testPalindromeRecursive( char *str, int len )
{
if ( len < 0 ) return 0;
return ( len < 2 ) ||
( str[0] == str[len-1] && testPalindromeRecursive( str + 1, len - 2 ) );
}
int main( void )
{
char s[] = "abbcccbba";
printf( "testPalindromeRecursive( \"%s\" ) is %s\n",
s, testPalindromeRecursive( s, sizeof( s ) - 1 ) ? "true" : "false" );
return 0;
}
The program output is
testPalindromeRecursive( "abbcccbba" ) is true
Take into account that you may adhere to the common convention according to which string functions do not check whether the passed character pointer is equal to NULL. It is the responsibility of the programmer to check this before the function call.
回答4:
My solution is capable of skipping whitespace:
int test_palindrom_recursive(char* str, int len)
{
if (len < 1) return 0;
int frontIndexToPass = 0, endIndexToPass = 0;
if (str[0] == ' ')
{
for (int front = 0; front < len - 1; front++)
{
if (str[front] == ' ') frontIndexToPass++;
else break;
}
}
if (str[len - 1] == ' ')
{
for (int end = len - 1; end >= 0; end--)
{
if (str[end] == ' ') endIndexToPass++;
else break;
}
}
if (tolower(str[0 + frontIndexToPass]) == tolower(str[len - endIndexToPass - 1]))
{
if (len <= 2) return 1;
else
test_palindrom_rekursiv(str + frontIndexToPass + 1,
len - endIndexToPass - frontIndexToPass - 2);
}
else return 0;
}
回答5:
This works just fine for me:
#include <stdio.h>
#include <string.h>
int testPalindromeRecursive(char* str, int len)
{
if (len <= 1)
return 1;
if (str[0] != str[len-1])
return 0;
return testPalindromeRecursive(str+1, len-2);
}
int main()
{
int i;
char *strs[5] = { "test", "tvt", "a", "palindrome", "racecar" };
for (i = 0; i < 5; i++)
printf("%s = %d\n", strs[i], testPalindromeRecursive(strs[i], strlen(strs[i])));
}
Edit: Fix according to comments to check for length==0 as well
回答6:
Using C#
I managed to get this:
int testPalindromeRecursive(string str, int len)
{
if (len <= 1)
return 0;
if (str[0] == str[len - 1])
{
str = str.Substring(1, len - 2);
return testPalindromeRecursive(str, str.Length);
}
return -1;
}
ref
is doing almost the same job as *
here. => Removed ref
because it was not the best option since it didn't allow the use of const
回答7:
[EDIT2]
This is the correct answer in C. Although it has been downvoted three times, I'm keeping it as it's the ONLY correct answer in C on this page.
[EDIT]
fixed my answer.
In C:
#include <stdio.h>
#include <string.h>
int testPalindromeRecursive(char* str, int len)
{
if (len <= 1)
return 0;
if (str[0] != str[len-1])
return 1;
return testPalindromeRecursive(str+1, len-2);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Usage: %s <string>\n", argv[0]);
return 1;
}
if (!testPalindromeRecursive(argv[1], strlen(argv[1])))
printf("Palindrom\n");
else
printf("Not palindrom\n");
return 0;
}
Running example for the case mentioned in a comment by kdopen (base case failure when testPalindromeRecursive("a", 1):
./palind a
Palindrom
More running examples mentioned by kdopen:
./mine \"a <-- the \ is to escape the " Not palindrom
./mine \"\" Palindrom
来源:https://stackoverflow.com/questions/30168953/recursive-method-for-palindrome-checkup