How do I circularly shift the alphabet without using circshift function?
5 views (last 30 days)
Show older comments
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 Comments
Answers (3)
Anand
on 21 Mar 2013
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 Comments
Azzi Abdelmalek
on 22 Mar 2013
Edited: Azzi Abdelmalek
on 22 Mar 2013
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 Comments
See Also
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!