Shifting letters by two positions. What's wrong with the code?
69 views (last 30 days)
I am trying to shift each letter of a given word by two positions.
inStr = 'doug';
a = find(inStr(i)==alphab)
a = (inStr(i)+rot)
a = rem(a,26);
A = alphab(a)
b = strcat(b,A);
Stephen23 on 11 Feb 2018
Edited: Stephen23 on 11 Feb 2018
Your basic mistake is that you assumed that the character code for 'a' is 1, and so on, which it is not. In fact 'a' has character code 97, 'b' has 98, etc. Lets have a look:
a = inStr(i)+rot
For example, given the letter 'c' the above line will produce a value of 99+2=101, whereas you assumed that it would produce the value 3, as this is clearly your assumption on the next few lines:
a = rem(a,26);
Actually all alphabetic characters have character code >26 (indeed all printable characters do), so this IF statement will always be true for alphabetic characters (upper or lowercase). If we take our example character 'c' + 2 = 101 then rem(101,26) gives 23, which is essentially a random number because there is no mathematical significance in the values of the character codes (i.e. absolute locations of characters in the tables), and certainly not in the remainder 26 of them. The only relevant information contained in the code table is the relative position of a character, but not its absolute position.
You then use this meaningless value as an index into alphab, which is a good idea, but you will need to take into account that MATLAB uses one-based indexing!
The output B is also not preallocated, which it could be quite simply.
So to fix these you first need to take into account the relative position of the character's character code, which you can do very simply by subtracting 'a' from it. Then apply mod (not rem as you can thus avoid the if statement), add one, and use that as the index:
inp = 'abcdwxyz';
rot = 2;
vec = 'a':'z';
out = inp; % preallocate!
for k = 1:numel(inp) % use NUMEL not LENGTH
tmp = inp(k)-'a'+rot; % -'a' to get relative position
out(k) = vec(1+rem(tmp,26)); % +1 for indexing
However this is MATLAB, so actually no loop is required:
>> inp = 'abcdwxyz';
>> rot = 2;
More Answers (1)
Walter Roberson on 11 Feb 2018
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.