Tuesday, 19 November 2013

MATLAB : PALINDROME


The following code demonstrates the 

% We iterate five times
for i = 1 : 5
    % Let's ask the user to enter digits one-by-one
    str = ['Give me digit number ' num2str(i) ': '];
    s(i) = input(str,'s');
    % and we reverse the digits, starting by the end
    p(6 - i) = s(i);
end
 
% Compare the entered string with the reversed one
if s == p
    % and answer accordingly
    disp ('WOW! Those digits happen to be a palindrome!')
else
    disp ('They are NOT a palindrome, sorry...')
end 

No comments:

Post a Comment