Check dates if palindrome
Mostrar comentarios más antiguos
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 comentarios
KSSV
el 18 de Nov. de 2022
Can you show us any example?
Delya Ochirova
el 18 de Nov. de 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.
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.
Respuestas (3)
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 comentarios
Jan
el 18 de Nov. de 2022
strcmp(yourString,yourString) is always true.
A simplification and correction:
function output = isPalindrome(yourString)
output = strcmp(yourString, flip(yourString));
end
The pattern:
if condition
result = true;
else
result = false;
end
can be simplified in general to:
result = condition;
which is even faster.
Image Analyst
el 18 de Nov. de 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.
Delya Ochirova
el 20 de Nov. de 2022
1 comentario
Jan
el 21 de Nov. de 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.
Categorías
Más información sobre Simulink Check en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!