repalacement of string with phone numbers

4 ビュー (過去 30 日間)
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 4 日
Hi; I am going to attempt that question: Each number on older telephone keypads, except 0 and 1, corresponds to three uppercase letters as shown in this list: 2 ABC, 3 DEF, 4 GHI, 5 JKL, 6 MNO, 7 PRS, 8 TUV, 9 WXY A phone-number specification can include uppercase letters other than Q and Z, digits, the # and * signs, spaces, parentheses and dashes. Write a function called dial that takes as its input argument a string of any length that includes only these characters and returns as its output argument a string containing the corresponding telephone number with only digits, spaces, and the # and * signs. Specifically, it does not change the digits, the # and * signs, or the spaces, but it replaces each parenthesis with a space and each dash with a space, and it converts each uppercase letter to a digit according to the list shown above. Here is the input and output for one example of a call of the function: Input: '1 (FUN) DOG-4-YOU #2' Output: '1 386 364 4 968 #2' Note that in this example there are three spaces in the input string and seven spaces in the output string. If the input does not meet the phone-number specification given above, the function returns an empty array. You are not allowed to use the built-in function strrep. I have no idea how make such function? help me... Thanks

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 6 月 4 日
編集済み: Andrei Bobrov 2015 年 6 月 4 日
function out = funbyMuhammad(in_char)
abc = ['()+-_',setdiff('A':'Z','QZ')];
d = ['#*','0':'9',' '];
if all(ismember(in_char,[abc,d]))
ii = [' ',sprintf('%d',kron(2:9,[1 1 1]))];
[lo,idx] = ismember(in_char,abc);
out = in_char;
out(lo) = ii(idx(lo));
else
out = [];
end
end
using
>> out = funbyMuhammad('1 (FUN) DOG-4-YOU #2')
out = 1 386 364 4 968 #2
>>
  7 件のコメント
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 4 日
編集済み: Muhammad Usman Saleem 2015 年 6 月 4 日
(y) for corrected 2 @ Andrei Bobrov thanks.............. :-D please mark some comments on funOnMatlab function so that i can understand this question batter....
Wasi von Deutschland
Wasi von Deutschland 2017 年 5 月 28 日
Hi @Andrei Bobrov Could you please tell me if Q and Z are allowed in the exercise then how one can do it? such as number 7 'PQRS' and number 9 'WXYZ' and number 8 as rest will work.

サインインしてコメントする。

その他の回答 (2 件)

Jan Orwat
Jan Orwat 2015 年 6 月 4 日
編集済み: Jan Orwat 2015 年 6 月 4 日
You can try regular expressions:
>> regexprep('1A23D4',{'A','D'},{'2','3'})
ans =
122334

liu yuhui
liu yuhui 2015 年 12 月 1 日
Thank you very much.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by