Caesar Cipher

Cym13 cpicard at openmailbox.org
Sun Feb 11 18:28:08 UTC 2018


On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:
> Hello there! I know deep Java, JavaScript, PHP, etc. but as you 
> all probably know, that's high-level and most of them only use 
> the heap memory.
>
> So I'm new to the wonderful world of low-level and the 
> stack-heap. I started a week ago learning D (which by the 
> moment is being easy for me) but I'm facing a big problem: I 
> don't know how to do the exercise of 
> https://tour.dlang.org/tour/en/basics/arrays . I really have 
> been looking on forums and Google but I found this in Java 
> https://stackoverflow.com/questions/10023818/shift-character-in-alphabet which is actually not the best due to Java uses other ways.
>
> My code is something like this:
>
> char[] encrypt(char[] input, char shift)
> {
>     auto result = input.dup;
>     result[] += shift;
>     return result;
> }
>
>
> What's wrong? I mean, I know that z is being converted into a 
> symbol, but how should I fix this?
> Thanks on forward.

Your mistake has little to do with D, and more with Ceasar (which 
is unfortunate IMHO): this cipher is usually defined only on the 
26 letters of the alphabet and seeing the result of the assert at 
the end of the code it's the case here. So while you're working 
on a full byte (256 values) you should restrict yourself to the 
26 lowercase ascii alpha characters.

Give it a try :)


More information about the Digitalmars-d-learn mailing list