How could I improve this code?

(Editing in progress)
Last edited on
Hi,

So you have subscripts 0 to 25, what to do with a number that exceeds 25 ? There is an operator that starts with m that deals with this.

Good Luck !!
I was thinking about that but couldn't figure out how to make it efficient, so I might need a little help with that :D. Besides that part is there anything I should do differently? Thanks in advance!
If I swap "The 42" by 1, I do get "if ". In other words, you do keep only the space and the lower-case letters. If all input matches that, then there is no problem.


Your inner loop looks at every character in 'characters' for each 'input' even though you know that at most one character can match the input. The std::string has member find().
http://www.cplusplus.com/reference/string/string/find/

However, characters can convert to integers. If the a-z are consecutive (like in ASCII), then
input[i] == characters[ input[i] - 'a' ] (assuming the input is in [a-z])


The 'output' starts empty and you append characters one by one. The string might do dynamic reallocation, when it grows. But you do a know that output probably grows to input.length().
You can do all necessary allocation in one step: http://www.cplusplus.com/reference/string/string/reserve/


TheIdeasMan did mention the modulo.
Interesting, thank you!
it would be marginally faster (if you did gigabytes of text at once) if you could combine the 2 loops into 1 loop and eliminate the if statement before the encryption, which would take some thinking to get the logic identical. if you could operate on integer sized blocks of bytes instead of one byte at a time, it would do a little better there too, eg take an int* into the string data and manipulate 8 bytes at once (for a 64 bit int).

Unless you plan on encrypting gigs and gigs of stuff, this isnt worth the trouble.
if you do want to encrypt gigs of stuff, just xor the data with a random number if you want speed, preferably in large integer sized chunks.



Thank you, thank you everyone! :)
Topic archived. No new replies allowed.