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

后端 未结 5 814
遇见更好的自我
遇见更好的自我 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:27

    One approach -

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

    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.

    Sample run -

    >> in_str,out_str
    in_str =
    adzC+*6AY&‘///\?abAB
    out_str =
    zwaX+*6ZB&‘///\?zyZY
    

提交回复
热议问题