Using Matlab
Write a function ispalindrome that returns whether a given input string is a palindrome and also its reversal (strip out any non-alphabetical characters). A palindrome is a string that is the same as its reverse. e.g., “Was it a car or a cat I saw?!” is a palindrome because you get the same string when you reverse the order of the characters. The first thing you do with the input is to strip out any non-alphabetical characters and convert any upper case letters to lower case. So, “Was it a car or a cat I saw?!” will become “wasitacaroracatisaw”. >> [ispal,rev]=ispalindrome(‘Was it a car or a cat I saw?!’) ispal = logical 1 rev = ‘wasitacaroracatisaw’ >> [ispal,rev]=ispalindrome(‘Fool proof!’) ispal = logical 0 rev = ‘foorploof’
Expert Answer
function [ispal,rev] = ispalindrome (ipString)
% MATLAB Function that tests whether given string is palindrome or not and returns reverse string
% Removing non-alphabetical characters.
ipString = regexprep(ipString,'[^w”]’,”);
% Convert any upper case letters to lower case
ipString = lower(ipString);
% Reversing a string
rev = ipString(length(ipString) : -1 : 1);
% Comparing strings
ispal = strcmp(ipString, rev);
endfunction
_________________________________________________________________________________________________
Sample Output: