Each number on telephone keypads, except 0 and 1, corresponds to a set of uppercase letters as shown in this list: 2 ABC, 3 DEF, 4 GHI, 5 JKL, 6 MNO, 7 PQRS, 8 TUV, 9 WXYZ Hence, a phone-number specification can include uppercase letters and

80 views (last 30 days)
REMAINING QUESTION: digits. Write a function called dial that takes as its input argument a char vector of length 16 or less that includes only these characters and returns as its output argument the telephone number as a uint64. Here is the input and output for one example of a call of the function: Input: '1FUNDOG4YOU' Output: 13863644968 You can assume that a phone number never starts with 0. If the input contains any illegal characters, the function returns 0. You are not allowed to use the built-in function strrep You can see code below. Do you know how one can add number 7 'PQRS' and 9 'WXYZ' and 8 'TUV'. THANKS
Wasi von Deutschland
Wasi von Deutschland on 27 May 2017
Stephen I know but I have problem solving it that's why I need your help, feedback and of course recommendations.

Sign in to comment.

Answers (3)

Stephen23 on 30 May 2017
Edited: Stephen23 on 3 Jun 2017
A much simpler solution (based on my comment to an earlier question):
function out = dial(inp)
vec = '012345678922233344455566677778889999';
[~,idb] = ismember(inp,dig);
out = sscanf(vec(idb),'%lu');
(to which it is easy to add input checks), and tested:
>> dial('1FUNDOG4YOU')
ans =

Sign in to comment.

J Philps
J Philps on 30 May 2017
Edited: Walter Roberson on 3 Jun 2017
Here is a sample:
function asNumbers = convertPhoneNum(asLetters)
% Make a cell array where each cell represents the letters for a number on the keypad.
lettersByIndex = {'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ'};
% We will build up this final string as the output.
asNumbers = ''; % Preallocate enough space to put the answer in.
% Iterate through the input
for i=1:length(asLetters)
% Check if this character is already a number.
if isstrprop(asLetters(i), 'digit')
asNumbers(i) = asLetters(i);
% If this character is not a number, find the corresponding
% alphabetical representation in the cell array.
% Sample Code:
for j=1:length(lettersByIndex)
% Check if the character can be found in that cell
if ~isempty(strfind(lettersByIndex{j}, asLetters(i))) % If letter is found in that group of letters
asNumbers(i) = num2str(j + 1);

Deepak Sharma
Deepak Sharma on 11 Sep 2017
@Stephen, Thanks for your answer. That's a very simple and beautiful solution. I am very new to Matlab (a few weeks) and I had solved the problem in a cumbersome way. You solution is very elegant.
@Wasi von Deutschland I think the answer would be a bit late for you, but might help someone in future. The answer (edited on 3rd June) by Stephen is almost 99% there. From the questions, there is only one thing remaining that needs to be handled: If there is any special character, return 0. Note that this will be uint64 (and not just say out = 0)
% Check if there are any invalid characters. If yes, return 0.
if sum(~ismember(inp,dig))>0
out = uint64(0);
Once you plug it in, it should work. Hope this helps.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!