Write a function ispalin s that returns 1 if the string s is

Write a function ispalin (s) that returns 1 if the string s is a palindrome, or 0 if not. A palindrome is a sentence that reads the same backward as forward, such as Reward a Toyota drawer (someone who draws Toyotas. presumably) or Napoleon\'s classic lament, Able was I ere I saw Elba. Assume there is no punctuation, remove all the blanks first, and convert all letters to lowercase.

Solution

There it goes the answer, it is a MATLAB function where, first af all the blanks are removed and then, all the letters are converted to lowercase. Then, the verification is done by using the function fliplr(\'string\'), that returns the string written backwards. With the if statements it is defined wether the string is a palindrome or not.

I have tested it and it works perfectly!

CODE OF THE FUNCTION

function [ y ] = ispalin( s )
s(s == \' \') = \'\'; %delete all spaces between the words
s = lower(s); % lowercase
%fliplr Flip matrix (or a string) in left/right direction.
if s== fliplr(s) %if the string is equal to the flipped string,
%it is a palindrome
   y=1;
else
   y=0;
end

end

EXAMPLES:

>> ispalin(\'Noon\')

ans =

1

>> ispalin(\'SolutionnoituloS\')

ans =

1

>> ispalin(\'Solution noituloS\')

ans =

1

>> ispalin(\'Solution noituloS forever\')

ans =

0

 Write a function ispalin (s) that returns 1 if the string s is a palindrome, or 0 if not. A palindrome is a sentence that reads the same backward as forward, s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site