我如何编写名为dedbi的MATLAB函数,该函数将输入xtx作为字符串并返回另一个字符串xtxx作为输出. [英] How can i write a MATLAB function named dedbi that takes input xtx as string and returns another string xtxx as output.
问题描述
dedbi颠倒了以下单词:"a"将被替换为"z","b"将被替换为"y","c"将被替换为"x",依此类推. dedbi将对大写字母执行相同的操作,即将字符串"A"替换为"Z",将"B"替换为"Y",将"C"替换为"X",依此类推.如果我给函数这个字符串'ab AB'函数应该返回'zy ZY',则输入除英语单词之外的其他字符,它将返回输入作为输出,输入为'///\?'将返回输出为'///\? '.
dedbi reverse the words that is, ‘a’ will be replaced by ‘z’, ‘b’ will be replaced by ‘y’, ‘c’ will be replaced by ‘x’ and so on. dedbi will do the same for capital letter that is, it will replace the string ‘A’ with ‘Z’, ‘B’ with ‘Y’, ‘C’ with ‘X’, and so on. If I give function this string ‘ab AB’ function should return ‘zy ZY’, input other than English words it will return input as output that is input as ‘///\?’ would return output as ‘///\?’.
到目前为止,我已经编写了这段代码.我必须承认,这个问题来自我需要通过的作业. 谢谢你们的好意.
So far i write this code. I must admit this problem is from an assignment that I need to pass. Thank You guys for your kind look.
function xtxx = dedbi(xtx)
txtt = char(xtx);
indexa = strfind(txtt,'a');
txtt(indexa) = 'z';
indA = strfind(txtt,'A');
txtt(indA) = 'Z';
indb = strfind(txtt,'b');
txtt(indb) = 'y';
indB = strfind(txtt,'B');
txtt(indB) = 'Y';
indc = strfind(txtt,'c');
txtt(indc) = 'x';
indC = strfind(txtt,'C');
txtt(indC) = 'X';
indd = strfind(txtt,'d');
txtt(indd) = 'w';
indD = strfind(txtt,'D');
txtt(indD) = 'W';
inde = strfind(txtt,'e');
txtt(inde) = 'v';
indE = strfind(txtt,'E');
txtt(indE) = 'V';
indf = strfind(txtt,'f');
txtt(indf) = 'u';
indF = strfind(txtt,'F');
txtt(indF) = 'U';
indg = strfind(txtt,'g');
txtt(indg) = 't';
indG = strfind(txtt,'G');
txtt(indG) = 'T';
indh = strfind(txtt,'h');
txtt(indh) = 's';
indH = strfind(txtt,'H');
txtt(indH) = 'S';
indi = strfind(txtt,'i');
txtt(indi) = 'r';
indI = strfind(txtt,'I');
txtt(indI) = 'R';
indj = strfind(txtt,'j');
txtt(indj) = 'q';
indJ = strfind(txtt,'J');
txtt(indJ) = 'Q';
indk = strfind(txtt,'k');
txtt(indk) = 'p';
indK = strfind(txtt,'K');
txtt(indK) = 'P';
indl = strfind(txtt,'l');
txtt(indl) = 'o';
indL = strfind(txtt,'L');
txtt(indL) = 'O';
indm = strfind(txtt,'m');
txtt(indm) = 'n';
indM = strfind(txtt,'M');
txtt(indM) = 'N';
indn = strfind(txtt,'n');
txtt(indn) = 'm';
indN = strfind(txtt,'N');
txtt(indN) = 'M';
indo = strfind(txtt,'o');
txtt(indo) = 'l';
indO = strfind(txtt,'O');
txtt(indO) = 'L';
indp = strfind(txtt,'p');
txtt(indp) = 'k';
indP = strfind(txtt,'P');
txtt(indP) = 'K';
indq = strfind(txtt,'q');
txtt(indq) = 'j';
indQ = strfind(txtt,'Q');
txtt(indQ) = 'J';
indr = strfind(txtt,'r');
txtt(indr) = 'i';
indR = strfind(txtt,'R');
txtt(indR) = 'I';
inds = strfind(txtt,'s');
txtt(inds) = 'h';
indS = strfind(txtt,'S');
txtt(indS) = 'H';
indt = strfind(txtt,'t');
txtt(indt) = 'g';
indT = strfind(txtt,'T');
txtt(indT) = 'G';
indu = strfind(txtt,'u');
txtt(indu) = 'f';
indU = strfind(txtt,'U');
txtt(indU) = 'F';
indv = strfind(txtt,'v');
indv(txtt) = 'e';
indV = strfind(txtt,'V');
txtt(indV) = 'E';
indw = strfind(txtt,'w');
txtt(indw) = 'd';
indW = strfind(txtt,'W');
txtt(indW) = 'D';
indx = strfind(txtt,'x');
txtt(indx) = 'c';
indX = strfind(txtt,'X');
txtt(indX) = 'C';
indy = strfind(txtt,'y');
txtt(indy) = 'b';
indY = strfind(txtt,'Y');
txtt(indY) = 'B';
indz = strfind(txtt,'z');
txtt(indz) = 'a';
indZ = strfind(txtt,'Z');
txtt(indZ) = 'A';
str = xtxx;
end
推荐答案
一种方法-
%// in_str: Input string
%// Logical masks for upper and lower case letters
upper_mask = in_str>=65 & in_str<=90 %// OR isstrprop(in_str,'upper')
lower_mask = in_str>=97 & in_str<=122 %// OR isstrprop(in_str,'lower')
%// Initialize output string
out_str = in_str
%// Replace upper letters with "flipped" upper letters; repeat for small letters
out_str(upper_mask) = char(in_str(upper_mask) + 2*(78-in_str(upper_mask))-1)
out_str(lower_mask) = char(in_str(lower_mask) + 2*(110-in_str(lower_mask))-1)
这些数字:78
和110
分别用作大写字母和小写字母范围的中间数字",并用于查找这两个类别中每个字母的差异.
Those numbers: 78
and 110
act as the "middle numbers" for the capital and small letters ranges respectively and are used for finding differences for each letter in those two categories.
样品运行-
>> in_str,out_str
in_str =
adzC+*6AY&‘///\?abAB
out_str =
zwaX+*6ZB&‘///\?zyZY
这篇关于我如何编写名为dedbi的MATLAB函数,该函数将输入xtx作为字符串并返回另一个字符串xtxx作为输出.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!