MATLAB includes functions upper and lower, which shift a string to upper case an
ID: 3766351 • 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 letter 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 3rd test.
Explanation / Answer
Here is the code for you. If you have any further queries, just revert here.
function cappedText = caps( text )
i = 1; %Starting from first character.
cappedText = lower(text); %copy all text in lower case.
display (text); %display given text.
cappedText(i) = upper(text(i)); %convert first character to upper case.
while(i <= length(text)) %For each character
if(cappedText(i) == ' ') %If it is a space.
cappedText(i+1) = upper(cappedText(i+1)); %Convert next character to uppercase.
i = i+1;
end
i = i + 1;
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.