MATLAB includes functions upper and lower, which shift a string to upper case an
ID: 653531 • Letter: M
Question
MATLAB includes functions upper and lower, which shift a string to upper case and lower case respectively. Create a new function called caps.m, which capitalizes the first and second letters in each word and forces all other letters to be lower case. (Hint: Take advantage of functions upper and lower.) The function must have one input argument and can handle the error cases of no input or two or more inputs. Use the following script file, test_caps.m, to call caps.m and print out before and after strings. Show your function file and the testing results of typing (1) THIS IS A TEST (2) this is a test (3) This iS a third test.
Script file: test_caps.m
string = input('Enter string: ','s');
fprintf('Before = %s ',string);
fprintf('After = %s ',caps(string));
Explanation / Answer
Matlab code to define functions upper and lower, which shift a string to upper case and lower case respectively:
string = input('Enter string: ','s');
fprintf('Before = %s ',string);
for i=1:length(string)
if(i ==1 or i==2)
string(1,i)=upper(string(1,i))
else
string(1,i)=lower(string(1,i))
end
end
fprintf('After = %s ',string);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.