solved

Feb 2, 2015 at 10:09am
done
Last edited on Feb 4, 2015 at 11:41am
Feb 2, 2015 at 10:23am
You need to index alphabet with the value of text[i], adjusted to be in the range 0-25:

1
2
3
4
    for (int i=0; i<text.length(); ++i)
    {
        text[i]=alphabet[text[i] - 'A'];
    }


And, you'll need to make sure the entirety of your input consists of capital letters.
Feb 2, 2015 at 10:34am
@cire Thanks a bunch! I implemented it and it works as you intended. Great to see it working.
From here on, I guess I'll think of changing my code so that it can encrypt both capital and lower-case letters as input.
Feb 2, 2015 at 11:31am
By the way, out of interest, what difference would it make if I store the Latin letters into a string instead into an array? I didn't think of it the first time and directly went with using an array, but now I changed it to a string and the result is the same.

Which one is better?
Feb 3, 2015 at 8:36am
Which one is better?


I don't think there's much difference with the code you have.

An alternate approach would be to use two strings. One string to relate a letter to an index. The other string to map that index to a different letter.

You could begin with the strings equal. Shuffle one. Search the unaltered one to find the letter you want to encode. The index you find it at should be used to index the other shuffled string to get the encoded version of the letter. (If you don't find the character in the string, perhaps just leave it unchanged.)

That approach has the advantage of being extensible. If you want to add a character to encode, you just add it to both strings at the beginning of the process.
Topic archived. No new replies allowed.