Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive method for palindrome checkup

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.

like image 852
neuronalbit Avatar asked Oct 16 '25 08:10

neuronalbit


2 Answers

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)

like image 177
kdopen Avatar answered Oct 18 '25 23:10

kdopen


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.

like image 31
pmg Avatar answered Oct 18 '25 22:10

pmg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!