Shifting letters by two positions. What's wrong with the code?
이전 댓글 표시
I am trying to shift each letter of a given word by two positions.
alphab='abcdefghijklmnopqrstwxyz'
inStr = 'doug';
rot=2
b=''
for i=1:length(inStr)
a = find(inStr(i)==alphab)
a = (inStr(i)+rot)
if abs(a)>26
a = rem(a,26);
end
A = alphab(a)
b = strcat(b,A);
end
댓글 수: 1
Simpler, and less liable to mistakes:
alphab = 'a':'z'
This line does nothing because its output is ignored:
a = find(inStr(i)==alphab)
채택된 답변
추가 답변 (1개)
Walter Roberson
2018년 2월 11일
0 개 추천
Hint: you can use the second output of ismember to give the index of the individual characters in the alphab .
Hint: a useful way to calculate wraparound is 1 + mod(value-1,base) . 26-1, mod 26 is 25, 25 + 1 is 26. 27-1, mod 26, is 0, 0 + 1 is 1.
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!