Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB includes functions upper and lower, which shift a string to upper case an

ID: 653344 • 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

string = input('Enter string: ','s'); # input a string from console
fprintf('Before = %s ',string); # printing the entered 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); # printing the modified string