2
$\begingroup$

Given a string such as "abcdefghijklmnopqrstuvwxyz", how would I go about returning a list of the alphabetical number of each letter in the string, example being 1,2,3,4,5,6,7,8,9 etc.?

$\endgroup$

2 Answers 2

4
$\begingroup$
1 - First@ToCharacterCode@"a" + ToCharacterCode@"abcdefghijklmnopqrstuvwxyz"

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}

$\endgroup$
2
$\begingroup$
str = "abcdefghijklmnopqrstuvwxyz"

p = StringPartition[str, 1]

{"a", "b", "c", etc.}

ord = Rule @@@ Transpose @ {Alphabet[], Range[Length@Alphabet[]]}

{"a" -> 1, "b" -> 2, "c" -> 3, etc.}

p /. rule

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}

$\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.