I have written this recursive function to determine if a string of text is a palindrome:
function [ palindromic ] = isPalindrome( string ) % base case #1
if length(string) < 1 palindromic = 1; return; end % base case #2
if string(1) ~= string(length(string)) palindromic = 0; return; end % recursive call
isPalindrome(string(2:length(string)-1)); end
When I pass it the string 'motor', it returns a 0 as it should because that string is not a palindrome. However, when I pass it the string 'rotor', the function returns nothing.
I was able to get the function to behave properly by adding the line
palindromic = 1;
to the start of the function, but I don't understand why this should make any difference.
Ideas?
Best Answer