How do I circularly shift the alphabet without using circshift function?
조회 수: 5 (최근 30일)
이전 댓글 표시
I'm trying to write a function that inputs the alphabet then shifts it by 3 so that then the original would be ABC...XYZ and then with the shift it'd look like XYZ...UVW.
Thanks for the help!
___________
Ok I typed this into matlab and it worked. I tried it again for a message that I want to shift 12 letters over, but it won't work.
lets say for example my message was: I HATE MATLAB.
How would i do a function so that the spaces and the punctuation would stay the same, but the message would then read: U TMFQ YMFXMN.
thanks again!
댓글 수: 0
답변 (3개)
Anand
2013년 3월 21일
alphabet = 97 : 122;
>> char(alphabet)
ans =
abcdefghijklmnopqrstuvwxyz
shifted_alphabet = alphabet-3;
shifted_alphabet(1:3) = alphabet(end-2:end);
>> char(shifted_alphabet)
ans =
xyzabcdefghijklmnopqrstuvw
댓글 수: 0
Azzi Abdelmalek
2013년 3월 22일
편집: Azzi Abdelmalek
2013년 3월 22일
shiftn=12
s='A':'Z';
str='I HATE MATLAB'
idx=regexp(str,'[A-Z]')
for k=idx
a=strfind(s,str(k))
str(k)=s(mod(a+shiftn,26))
end
%or
shiftn=12
str='I HATE MATLAB'
sth=double(str)-64
idx=regexp(str,'[A-Z]')
sth(idx)=mod(sth(idx)+shiftn,26)
out=char(sth+64)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Compiler에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!