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

% Function Name: phoneConvert % Inputs (1): - (char) A string representing a pho

ID: 3537016 • Letter: #

Question

% Function Name: phoneConvert
% Inputs (1): - (char) A string representing a phone number that contains
%                        letters
% Outputs (1): - (char) The same telephone number with all letters
%                        converted to their corresponding numbers (as found
%                        on a telephone)
%
% Function Description:
%   If you have seen phone numbers that contain letters instead of numbers,
%    you may have thought "I wish I could know the numbers that correspond
%    to those letters so I can dial the number!" With the help of MATLAB,
%    now you can! Write a function called "phoneConvert" that takes in a
%    string containing a phone number and returns a string that contains the
%    same telephone number in a different format. The instructions and
%    formatting for the inputs and outputs can be found below:
%
%   Input Format:
%     - The input will either start with an "area code" in the format "###-"
%       or a "Country code" followed by an area code in the format
%       "##-###-" or "#-###-" (where the # character is used here to denote
%       any number between 0 and 9, inclusive)
%     - The remainder of the input string will contain AT LEAST 7 LETTERS
%     (Can be either lower or upper case)
%
%     - Examples of an inputs are '1-800-SilphCo' and '69-321-oMgSoLeEtQQ'
%
%   Specifics:
%     - Convert all letters to their respective numbers (as found on a
%       telephone keypad)
%     - Retain only the first 7 numbers of the letter portion which will
%       later be inserted after the area code
%     - Place a hyphen between the 3rd and 4th number (of the 7 given in
%       the previous instruction)
%     - Replace all letters in the phone number with these new numbers
%
%   OUTPUT FORMAT:
%     - The output string should look like either '##-###-###-####' or
%       '###-###-####' or '#-###-###-####" (where the # character is used
%       here to denote any number between 0 and 9, inclusive)
%     - For a listing of which letters correspond to which numbers, you
%       should consult the following website or the nearest telephone
%       keypad:
%
% http://upload.wikimedia.org/wikipedia/commons/4/43/Telephone-keypad.svg
%
% Hints:
%     - You may find the strtok() or strfind() functions useful
%
% Test Cases:
%   pn1 = phoneConvert('01-800-ThisWasATriumph');
%       pn1 => '01-800-844-7927'
%
%   pn2 = phoneConvert('229-AintNobodyGotTimeForThat');
%       pn2 => '229-246-8662'
%
%    pn3 = phoneConvert('89-258-ohHEYpikachuIchooseYouGOOOOOO');
%       pn3 => '89-258-644-3974'

Explanation / Answer

function newNum = phoneConvert(str)

[t,r]= strtok(fliplr(str),'-');

t=fliplr(t);

r=fliplr(r);

t=lower(t);

t=regexprep(t,'a|b|c','2');

t=regexprep(t,'d|e|f','3');

t=regexprep(t,'g|h|i','4');

t=regexprep(t,'j|k|l','5');

t=regexprep(t,'m|n|o','6');

t=regexprep(t,'p|q|r|s','7');

t=regexprep(t,'t|u|v','8');

t=regexprep(t,'w|x|y|z','9');

newNum=strcat(r,t(1:3),'-',t(4:7));

end