# Converting set of characters into numbers using list of replacement rules

I am trying to convert a text, as an example "aaaa" into a set of numbers as defined in a list of replacement rules, for example letterstonum . letterstonum takes in letters a to z and returns the respective number from 1 to 26.

it works fine when tried with individual letters for instance:

Replace[a, letterstonum]


1 was returned

What I want to do is the same for a set of characters, for instance aaaa or abjdfjs .

I tried the following:

ReplaceAll[Characters["aaaaa"], letterstonum]


but the only return I got was {"a", "a", "a", "a", "a"}

which is the same I get for evaluating[Characters["aaaaa"]

My desired output in this case, would be 11111 or {1,1,1,1,1}

Is there something I'm doing wrong or missing?

Any help would be much appreciated

here's letterstonum :

letterstonum = {a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6,
g -> 7, h -> 8, i -> 9, j -> 10, k -> 11, l -> 12, m -> 13, n -> 14,
o -> 15, p -> 16, q -> 17, r -> 18, s -> 19, t -> 20, u -> 21,
v -> 22, w -> 23, x -> 24, y -> 25, z -> 26}

• It appears you have a local set of rules called letterstonum defined. Please post them. – Jack LaVigne Oct 13 '15 at 0:26
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Oct 13 '15 at 0:33

After taking the string and making individual characters you can apply LetterNumber to each character. This can be done by Map.

Map[LetterNumber, Characters["aaaaa"]]


produces

{1, 1, 1, 1, 1}

• Thank you for your reply, it works, what do I do if I want to get an output in the form of 11111 – Vihanga Gamage Oct 13 '15 at 0:37
• Is there a way to have the same effect as Map[LetterNumber, Characters["aaaaa"]] using my current set of rules(letterstonum) . I've added it to the post – Vihanga Gamage Oct 13 '15 at 0:42
• Neat. Will be gd if it can distinguish lower case & upper case characters – thils Oct 13 '15 at 4:44

When 1 is returned in the first case, it means you have replaced symbol a to 1 in your letterstonum. I guess your letterstonum is something like below

letterstonum = {a -> 1, b -> 2}


In the second case, the "a" is not symbol but character. Hence, if you want {1,1,1,1,1} in your second case, you need to replace character a to 1 like below

letterstonum = {"a" -> 1, "b" -> 2}

• Thank you for your reply. And, yes, you're right, that is what my current letterstonum looked like, and changing it did the trick. Could you please tell me if there is anyway to do it with the initial one? and how can I get the output as 11111? – Vihanga Gamage Oct 13 '15 at 0:39
• Ok there maybe better answers but here is quick fix. letterstonum = {"a" -> "1", "b" -> "2"} ReplaceAll[Characters["aaaaa"], letterstonum] InputForm[%] StringJoin[%] – Prashanth Oct 13 '15 at 0:56
letterstonum = {a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6,
g -> 7, h -> 8, i -> 9, j -> 10, k -> 11, l -> 12, m -> 13,
n -> 14, o -> 15, p -> 16, q -> 17, r -> 18, s -> 19, t -> 20,
u -> 21, v -> 22, w -> 23, x -> 24, y -> 25, z -> 26};

FromDigits@("aaaaa" // Characters) /.
(Replace[letterstonum,
x_Symbol :> ToString@x, 1])

(*  11111  *)


With ToCharacterCode

One can use the ASCII code of the letters properly shifted so as to start with 1 for a:

ltn[st_] := ToCharacterCode[st] - 96


For the string input "aaaaa":

ltn["aaaaa"]
(* {1, 1, 1, 1, 1} *)


With letterstonum

To use letterstonum as posted (i.e. with symbols rather than strings for the letters), a possible approach is

ltn2[s_] := ToExpression@Characters@ToString[s] /. letterstonum


This returns for the inputs aaaaa and "aaaaa"

ltn2[aaaaa]
(* {1, 1, 1, 1, 1} *)

ltn2["aaaaa"]
(* {1, 1, 1, 1, 1} *)


To obtain an output of the form 11111, you can consider instead

ltn2[s_] := FromDigits[ToExpression@Characters@ToString[s] /. letterstonum]


According to the original post, you have a list

letterstonum = {a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, ... };


In that case, do

StringReplace["abracadabra", MapAt[ToString, letterstonum, {All, {1, 2}}]]
(* "1218131412181" *)


However, based on one of the OP's comments, it seems it is actually a list of rules that looks like this:

letterstonum = {"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5, ... };


In that case, these two options work:

StringReplace["abracadabra", ReplacePart[#, 2 -> ToString@Last@#] & /@ letterstonum]

Finally, I would just from the beginning define letterstonum as
letterstonum = {"a" -> "1", "b" -> "2", "c" -> "3", "d" -> "4", "e" -> "5", ... };

StringReplace["abracadabra", letterstonum]