sum=0;
n1=input('Please enter first date:');
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
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

KSSV
KSSV 2022년 11월 18일
Can you show us any example?
Delya Ochirova
Delya Ochirova 2022년 11월 18일
for example i want interval 01.01.1900-01.01.2023. this is my limits. program should check all dates from my input data(n1-first, n2-second) and print all palindromes
Jan
Jan 2022년 11월 18일
편집: Jan 2022년 11월 18일
@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
Jan 2022년 11월 18일
편집: Jan 2022년 11월 18일
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.

댓글을 달려면 로그인하십시오.

답변 (3개)

KSSV
KSSV 2022년 11월 18일
편집: KSSV 2022년 11월 18일

0 개 추천

t = datetime(2023,3,20) ;
t.Format = 'Mddyy' ;
if isPalindrome(char(t))
fprintf('%s is a Palindrome\n',char(t))
end
32023 is a Palindrome
function output = isPalindrome(yourString)
reverseString = flip(yourString) ;
if strcmp(yourString,reverseString)
output = true;
else
output = false;
end
end

댓글 수: 3

Jan
Jan 2022년 11월 18일
strcmp(yourString,yourString) is always true.
A simplification and correction:
function output = isPalindrome(yourString)
output = strcmp(yourString, flip(yourString));
end
KSSV
KSSV 2022년 11월 18일
@Jan Aboslutely yes. I missed this.
Jan
Jan 2022년 11월 18일
편집: Jan 2022년 11월 18일
The pattern:
if condition
result = true;
else
result = false;
end
can be simplified in general to:
result = condition;
which is even faster.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2022년 11월 18일

0 개 추천

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
Delya Ochirova 2022년 11월 20일

0 개 추천

close all
clear
clc
t1=input('First date:');
t2=input('Second date:');
format='ddmmyyyy';
n1=datenum(t1,format);
n2=datenum(t2,format);
for i=n1:n2
n3=datestr(i,format);
if n3==fliplr(n3)
fprintf ('Palindrome dates:%s\n', n3)
end
end
i dont understand why its not working.

댓글 수: 1

Jan
Jan 2022년 11월 21일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Simulink Check에 대해 자세히 알아보기

태그

질문:

2022년 11월 18일

댓글:

Jan
2022년 11월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by