Check dates if palindrome
5 views (last 30 days)
Show older comments
sum=0;
n1=input('Please enter first date:');
n2=input('Please enter second date:');
x=n1:n2;
for i=n1:n2
if (n1>0)
r=round(rem(n1,10)-0.1);
sum=sum*10+r;
n1=round((n1/10)-0.1);
c1=x-sum;
elseif (n2>0)
r=round(rem(n2,10)-0.1);
sum=sum*10+r;
n2=round((n2/10)-0.1);
c2=x-sum;
end
end
Im trying to write the code which is checking palindrome in the interval of dates. how can i put the limits. and then it should print all palindromes one after other.
4 Comments
Jan
on 18 Nov 2022
Edited: Jan
on 18 Nov 2022
@Delya Ochirova: What do you call "a date" and what are typical inputs of this code? The input() command is not a good choice to explain a problem. Prefer posting explicit example data.
Avoid to use "sum" as a name of a variable, because this causes troubles, if you want to use the function with the same name later.
Jan
on 18 Nov 2022
Edited: Jan
on 18 Nov 2022
Okay. How yould you use input() to get something like "01.01.1900" ? What do you call a palindrome of e.g. "01.01.1900"? "0091.10.10" is not an option, so please define exactly, what you want.
Because I dared to guess, what you want, I've wasted some time already withwriting a not matching answer.
Answers (3)
KSSV
on 18 Nov 2022
Edited: KSSV
on 18 Nov 2022
t = datetime(2023,3,20) ;
t.Format = 'Mddyy' ;
if isPalindrome(char(t))
fprintf('%s is a Palindrome\n',char(t))
end
function output = isPalindrome(yourString)
reverseString = flip(yourString) ;
if strcmp(yourString,reverseString)
output = true;
else
output = false;
end
end
3 Comments
Image Analyst
on 18 Nov 2022
Are you considering month/day/year separators to be included in the string? Try this where I do it both ways.
% Get user's date:
date1 = input('Please enter first date:', 's');
% Now see if it's a palindrome leaving the separators in.
if isequal(date1, flip(date1))
fprintf('"%s" is a palindrome date with using separators.\n', date1);
else
fprintf('"%s" is not a palindrome date with using separators.\n', date1);
end
% Now see if it's a palindrome with numbers only,
% ignoring the separators.
pat = digitsPattern;
numbersOnly = join(extract(date1, pat));
numbersOnly = strrep(numbersOnly{1}, ' ', '');
if isequal(numbersOnly, flip(numbersOnly))
fprintf('"%s" is a palindrome date without using separators because it is %s.\n', date1, numbersOnly);
else
fprintf('"%s" is not a palindrome date without using separators because it is %s.\n', date1, numbersOnly);
end
Here are some examples:
Please enter first date:2/2/2022
"2/2/2022" is not a palindrome date with using separators.
"2/2/2022" is not a palindrome date without using separators because it is 222022.
Please enter first date:1/11/11
"1/11/11" is not a palindrome date with using separators.
"1/11/11" is a palindrome date without using separators because it is 11111.
0 Comments
Delya Ochirova
on 20 Nov 2022
1 Comment
Jan
on 21 Nov 2022
If it does not work, this is not an answer.
I asked you 3 days ago, what you call a palindrom e.g. for the input "01.01.1900". It would be much easier to help you, if you mention at least, what you exactly want. Posting some code, which does not work, is not useful.
The readers cannot know, what you input as n1 and n2.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!