Hi,
So I'm working on this mono-alphabetic substitution system, which - for the people unfamiliar with it - basically means that you substitute every letter with another one in the alphabet using a key.
It's the most basic form of encryption.
The problem I'm having (I mean it works, but seems rather inefficient) is that you can't just loop through the alphabet and replace each character with it's substitute, because you would come to a point where you replace an already replaced character.
For example:
- Take the string 'abcde'
- Replace occurences of 'a' with 'b' - resulting into 'bbcde'
- And now replace occurences of 'b' with 'e' - resulting into 'eecde'
Where the desired result would be 'becde'.
To solve this I've used the following:
It works, but if the text is long enough this solution is rather slow.Code:for (int i = 0; i < keyLength; i++) { for (int j = 0; j < textLength; j++) { if (text[j] == key[i]) decoded[j] = this._referenceKey[i]; } }
Is there a nicer solution for something like this?
Thanks!![]()




Reply With Quote