5
$\begingroup$
ToCharacterCode["ABCD...XYZ"] - 64 

I know that I can make a list of alphabets in this way.

However, I need to assign " " and "." too.

Like " "=27 and "."=28

For example "I AM A BOY." = {9,27,1,13,27,1,27,2,15,25,28}

$\endgroup$
1
  • 4
    $\begingroup$ maybe ToCharacterCode["I AM A BOY."] - 64 /. {-32 -> 32, -18 -> 46}? $\endgroup$ Commented Apr 28, 2017 at 13:56

2 Answers 2

4
$\begingroup$
ToCharacterCode["I AM A BOY."] - 64 /. {-32 -> 32, -18 -> 46} 

{9, 32, 1, 13, 32, 1, 32, 2, 15, 25, 46}

where " " and "." are mapped to their respective character codes. If you wish to map " " to 27 and "." to 28, you can use

ToCharacterCode["I AM A BOY."] - 64 /. {-32 -> 27, -18 -> 28} 

{9, 27, 1, 13, 27, 1, 27, 2, 15, 25, 28}

$\endgroup$
2
$\begingroup$

You can do it with PositionIndex and just build the string using CharacterRange:

Catenate@Lookup[ PositionIndex[Characters[CharacterRange["A", "Z"] <> " ."]], Characters["I AM A BOY."]] (* {9, 27, 1, 13, 27, 1, 27, 2, 15, 25, 28} *) 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.