How can i write a MATLAB function named dedbi that takes input xtx as string and returns another string xtxx as output.

后端 未结 5 849
遇见更好的自我
遇见更好的自我 2021-01-22 09:38

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 t

5条回答
  •  没有蜡笔的小新
    2021-01-22 10:32

    Here's a way to do that:

    alphabet = 'abcdefghijklmopqrstuvwxyz';
    ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    ralphabet = fliplr(alphabet);
    RALPHABET = fliplr(ALPHABET);
    
    txt = 'abc d ?!.< n AC';
    
    [~, index] = ismember(txt,alphabet);
    target  = index ~= 0;
    src     = index(target);
    
    [~, INDEX] = ismember(txt,ALPHABET);
    TARGET  = INDEX ~= 0;
    SRC     = INDEX(TARGET);
    
    txtInvert = txt;
    txtInvert(target) = ralphabet(src);
    txtInvert(TARGET) = RALPHABET(SRC);
    

    Output:

    zyx w ?!.< n ZX

提交回复
热议问题