How do I "backward" a string in matlab: turn "a" into "z", "b" into "y", "c" into "x", etc.

조회 수: 6 (최근 30일)
I used the following code, but got an error...
function output_string = codeit(txt);
for ii = 1:length(txt)
input_char = txt(ii);
if input_char>='a'&&input_char<='z';
reverse_alphabet= ['z' 'y' 'x' 'w' 'v' 'u' 't' 's' 'r' 'q' 'p' 'o' 'n' 'm' 'l' 'k' 'j' 'i' 'h' 'g' 'f' 'e' 'd' 'c' 'b' 'a'];
reverse_alphabet_index = input_char - 'a' + 1;
output_char = reverse_alphabet(reverse_alphabet_index);
output_string(ii) = output_char;
elseif input_char>='A'&&input_char<='Z';
reverse_alphabet= ['Z' 'Y' 'X' 'W' 'V' 'U' 'T' 'S' 'R' 'Q' 'P' 'O' 'N' 'M' 'L' 'K' 'J' 'I' 'H' 'G' 'F' 'E' 'D' 'C' 'B' 'A'];
reverse_alphabet_index = input_char - 'A' + 1;
output_char = reverse_alphabet(reverse_alphabet_index);
output_string(ii) = output_char;
end
end
end
ans =
Feedback: Your function performed correctly for argument(s) 'azAZ'
Feedback: Your program made an error for argument(s) '`_^]\[ {|}~'
Your solution is _not_ correct.
  댓글 수: 3
Stephen23
Stephen23 2015년 6월 1일
편집: Stephen23 2015년 6월 1일
@Darshan Kanungo: What is the specified/expected output for the input string '`_^]\[ {|}~' ? This is currently not specified in the question or the error message, so we have no idea how the function should behave with non-alphabetic characters. You need to specify this for us!
Darshan Kanungo
Darshan Kanungo 2015년 6월 1일
Write a function called codeit that takes a string txt as input and returns another string coded as output. The function reverses the alphabet: It replaces each 'a' with 'z', each 'b' with 'y', each 'c' with 'x', etc. The function must work for uppercase letters the same way, but it must not change any other characters. Note that if you call the function twice like this str = codeit(codeit(txt)) then str and txt will be identical.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2015년 6월 1일
편집: Stephen23 2015년 6월 1일
You could use char:
>> char(219-'abc')
ans =
zyx
Note this assumes that the text consists only of lower-case alphabetic characters. If you want mixed case then this will be much more complicated.
EDIT in response to the OP completely rewriting their question, removing the original example and stating new requirements.
The new code given in the question is very complicated: solving this in a loop is slow and a waste of MATLAB's code vectorization abilities. Here is a completely vectorized function based on isstrprop that reverses only the alphabetic characters (keeping case the same) and ignores all other characters:
function str = str_rev(str)
idx = isstrprop(str,'upper');
str(idx) = char(155-str(idx));
idx = isstrprop(str,'lower');
str(idx) = char(219-str(idx));
end
And some examples showing it in action:
>> str_rev('abc')
ans =
zyx
>> str_rev('AbcD')
ans =
ZyxW
>> str_rev('Hello World!')
ans =
Svool Dliow!

추가 답변 (2개)

Jan
Jan 2015년 6월 1일
function out = codeit(in)
lut = char(1:255);
lut(['a':'z', 'A':'Z']) = ['z':-1:'a', 'Z':-1:'A'];
out = lut(in);

charu sharma
charu sharma 2015년 8월 26일
  댓글 수: 1
Guillaume
Guillaume 2015년 8월 26일
I wouldn't classify this answer as spam, the link is relevant to the question (as long as it does not disappear).
However, the answer posted in the link is a poor example of coding (unnecessary loops, unnecessary conversions, unnecessary use of temporary variables), so is of little value.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by