2
$\begingroup$

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]]]
$\endgroup$
2
  • 3
    $\begingroup$ What have you tried? Hint: ToUpperCase@Alphabet[] $\endgroup$
    – C. E.
    Commented Jun 11, 2020 at 20:20
  • $\begingroup$ @C.E. Yes I tried that in any ways and variations of StringReplace whitous success. I didn´t type in the message but I tried before post the question. I know that it´s a good practise comment the efforts. $\endgroup$
    – Mika Ike
    Commented Jun 12, 2020 at 7:10

3 Answers 3

3
$\begingroup$

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"

$\endgroup$
1
  • $\begingroup$ My problem was in joining the Thread´s $\endgroup$
    – Mika Ike
    Commented Jun 12, 2020 at 7:15
1
$\begingroup$

How about?

step=1;
ToUpperCase@StringReplace[ToLowerCase["This is a String"],Thread[Alphabet[]->RotateLeft[Alphabet[],step]]]

(* UIJT JT B TUSJOH *)
```
$\endgroup$
5
  • $\begingroup$ I don't think OP wants all letters to be uppercase. He just wants to extend his existing code so that it handles the capital letters in it that already exist (in this case T and S), and preserves the case of those letters. $\endgroup$
    – C. E.
    Commented Jun 12, 2020 at 5:15
  • $\begingroup$ In all fairness, he didn't say. Plus, have you ever written true coded messages? Usually, they are all uppercase or have letters missing. $\endgroup$
    – prog9910
    Commented Jun 12, 2020 at 13:41
  • $\begingroup$ To me his question is clear. He had a solution that replaced all lowercase letters but left the capital letters in his example untouched. He asked ”how to add the capital letters”. It’s important to note that his example code does not work, and adding rules for capital letters as in my answer is what fixes it. From my point of view, the example and the question together leaves little room for interpretation. $\endgroup$
    – C. E.
    Commented Jun 12, 2020 at 13:57
  • $\begingroup$ Well then, I guess we agree to disagree. You should get the OP to state, what he whats only. Then, you can delete my attempted answer. That is all it ever was. $\endgroup$
    – prog9910
    Commented Jun 12, 2020 at 14:00
  • $\begingroup$ To be clear: I don’t think your question should be deleted. I often leave small comments like the one above when I see what I think may be a misinterpretation to give the author a chance to update his answer if he wishes, and for other reasons. But it shouldn’t be seen as trying to pressure the author into doing something, that’s not my goal at all. $\endgroup$
    – C. E.
    Commented Jun 12, 2020 at 14:04
1
$\begingroup$

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 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.