Part 2: String Handling In the same script as part 1, ask the user to enter two
ID: 3737564 • Letter: P
Question
Part 2: String Handling In the same script as part 1, ask the user to enter two dates and then indicate which date comes earlier on the calendar. No function is needed for this part. EXAMPLE: Enter first date (mm/dd/yy) 3/6/08 Enter second date(mm/dd/yy): 5/17/07 The output 5/17/07 is earlier than 3/6/08 1. Prompt the user for the first date. Read the date and check if it is valid. If it is not valid, print a suitable error message and go onto part3. 2. Prompt the user for the second date. Read the date and check if it is valid. If it is not valid, print a suitable error message and go on to part3. 3. If valid dates print the output as shown above. (hint: strarray strsplit(str.delimiter) splits str at the delimiters specified by delimiter. You may need char function and str2mum function.)Explanation / Answer
SAVE THE FOLLOWING CODE IN MATLAB AND GET THE RESULTS-
clc
clear
close all;
x=input('Enter the first date');% d = '03-06-2008'
y=input('Enter the second date');% d = '05-17-2007'
% Checking the validity of the date
fmt='dd mmm yyyy';
dt=[];
while isempty(dt)
x=input(['enter date string in ',fmt,' format: '],'s');
try
dt=datenum(x,fmt);
catch
disp(['Hey idiot, I said ',fmt,' format'])
end
end
% Checking which date comer earlier
A=x(7:10);
B=y(7:10);
if A>B
disp('x comes first')
else
disp('y comes first')
end
SOLUTION-
After saving the program, run the program and type the following commands in the command window as asked step by step-
>> Enter the first date'03-06-2008'
>> Enter the second date'05-17-2007'
>> enter date string in dd mmm yyyy format: 03 09 2009
>. y comes first
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.