This code only works for lowercase letters, how can you add the capital letters?
step = 1;
StringReplace["This is a String",
Thread[Alphabet[] -> RotateLeft[Alphabet[], step]]]
I think you can work this out on your own so I wasn't intending to answer it, but I'll answer it since I'm not satisfied with the other answers so far. This is the simplest extension, in my mind, to your solution, that I intended for you to discover when I posted my comment:
rules = Join[
Alphabet[] -> RotateLeft[Alphabet[], 1] // Thread,
ToUpperCase@Alphabet[] -> RotateLeft[ToUpperCase@Alphabet[], 1] // Thread
];
StringReplace["This is a String", rules]
"Uijt jt b Tusjoh"
How about?
step=1;
ToUpperCase@StringReplace[ToLowerCase["This is a String"],Thread[Alphabet[]->RotateLeft[Alphabet[],step]]]
(* UIJT JT B TUSJOH *)
```
This builds on your code but preserves the original case:
caesar[str_, step_] := Block[{
casevec = UpperCaseQ /@ Characters[str],
subs = StringReplace[ToLowerCase@str, Thread[Alphabet[] -> RotateLeft[Alphabet[], step]]]},
StringJoin@MapThread[If[#2, ToUpperCase[#1], #1] &, {Characters[subs], casevec}]]
caesar["This is a String", 1]
(* returns: Uijt jt b Tusjoh *)
ToUpperCase@Alphabet[]
$\endgroup$