Function Name: switchCase % Inputs (2): - (char) a string containg a word or phr
ID: 3536973 • Letter: F
Question
Function Name: switchCase
% Inputs (2): - (char) a string containg a word or phrase
% - (char) a string determining how to modify the previous
% input
% Outputs (1): - (char) the modified string
%
% Function Description:
% Write a function called "switchCase" that takes in a string containing
% a word or phrase and another string containing directions, and outputs
% the modified word or phrase based on the directions. There can be 4
% different ways of modifying the string:
%
% - 'Title Case' -> Capitalizes the first letter of every word.
% Everything else is lowercase.
% - 'Camel Case' -> Capitalizes the first letter of every word
% EXCEPT the first one. Everything else is
% lowercase. All the spaces are removed.
% - 'Da Vinci' -> Reverses the string.
% - '1337' -> Changes each of the following letters into
% it's respective number replacement.
% '1' should replace 'l'
% '2' should replace 'z'
% '3' should replace 'e'
% '4' should replace 'a'
% '5' should replace 's'
% '6' should replace 'g'
% '7' should replace 't'
% '8' should replace 'b'
% '9' should replace 'j'
% '0' should replace 'o'
% Each of the replacements should work for
% capitalized letters as well.
%
%
% Test Cases:
% new_string = switchCase('there AND back again', 'Title Case');
% new_string => 'There And Back Again'
%
% new_string = switchCase('What is better than UPPER or lower case?',...
% 'Camel Case');
% new_string => 'whatIsBetterThanUpperOrLowerCase?'
%
% new_string = switchCase('Hannah!', 'Da Vinci');
% new string => '!hannaH'
%
% new_string = switchCase('Now I can talk like a REAL hacker!', '1337');
% new_string => 'N0w I c4n 741k 1ik3 4 R341 h4ck3r!'
Explanation / Answer
function newstr = switchCase(str,c)
switch(c)
case 'Title Case'
str= lower(str);
newstr = regexprep(str,'(<[a-z])','${upper($1)}');
case 'Camel Case'
str= lower(str);
str = regexprep(str,'(<[a-z])','${upper($1)}');
str = regexprep(str,'^s*.','${lower($0)}');
newstr = regexprep(str,'s*','');
case 'Da Vinci'
newstr=fliplr(str);
case '1337'
str=regexprep(str,'l|L','1')
str=regexprep(str,'a|A','4')
str=regexprep(str,'z|Z','2')
str=regexprep(str,'e|E','3')
str=regexprep(str,'s|S','5')
str=regexprep(str,'g|G','6')
str=regexprep(str,'t|T','7')
str=regexprep(str,'b|B','8')
str=regexprep(str,'j|J','9')
newstr=regexprep(str,'o|O','0')
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.