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 toupper case and

ID: 3642173 • Letter: M

Question

MATLAB includes functions upper and lower, which shift a string toupper case and lower case respectively. Create a new function called caps, which capitalizes the first letter in each wordand forces all other letters to be lower case. (Hint:Take advantage of functions upper,lower, and strtok.) At a minimum run the following 3 test cases, enter

Explanation / Answer

caps.m function [ out_string ] = caps( in_string ) %caps(in_string) returns in_string with the first letter of each word % capitalized and all other letters lowercase in_string=lower(in_string); % convert in_string to lowercase [token, remain]=strtok(in_string); % split off first word token(1)=upper(token(1)); % uppercase first letter out_string=token; % add word to output while not(isempty(remain)) % loop through entire string [token, remain] = strtok(remain); % split off next word token(1) = upper(token(1)); % uppercase first letter of word out_string = [out_string, ' ' ,token]; % add word to output end end Test results: >> test1 = 'THIS IS A TEST'; >> test2 = 'this is a test'; >> test3 = 'This iS a 3rd test'; >> test4 = 'tHiS'; >> caps(test1) ans = This Is A Test >> caps(test2) ans = This Is A Test >> caps(test3) ans = This Is A 3rd Test >> caps(test4) ans = This