# 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)

Show older comments

### Answers (3)

Stephen23
on 30 May 2017

Edited: Stephen23
on 3 Jun 2017

function out = dial(inp)

dig = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

vec = '012345678922233344455566677778889999';

[~,idb] = ismember(inp,dig);

out = sscanf(vec(idb),'%lu');

end

(to which it is easy to add input checks), and tested:

>> dial('1FUNDOG4YOU')

ans =

13863644968

##### 14 Comments

Deepak Sharma
on 11 Sep 2017

Edited: Deepak Sharma
on 11 Sep 2017

@Stephen Cobeldick: Thanks, this was a very elegent solution.

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.

else

% 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);

end

end

%}

end

end

##### 0 Comments

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);

return;

end

Once you plug it in, it should work. Hope this helps.

##### 5 Comments

SULE SAHIN
on 7 Nov 2017

@Stephen Cobeldick I do anything, ı suppose that I will not solve this question Thank you

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!