Shifting letters by two positions. What's wrong with the code?
Ältere Kommentare anzeigen
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 Kommentar
Simpler, and less liable to mistakes:
alphab = 'a':'z'
This line does nothing because its output is ignored:
a = find(inStr(i)==alphab)
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 11 Feb. 2018
0 Stimmen
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.
Kategorien
Mehr zu Common Operations finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!