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$
    – kglr
    Apr 28 '17 at 13:56
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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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