# Convert letters to numbers in C programming.

I know there is a way to do this in C++, but I don't know in C.

I am using a double scripted array and the user inputs coordinates seperated by a space. What I want the user to enter is something like 'A 1' or 'B 1'. How could I convert the user inputted 'A' into the number 1?

• : I know there is a way to do this in C++, but I don't know in C.
:
: I am using a double scripted array and the user inputs coordinates seperated by a space. What I want the user to enter is something like 'A 1' or 'B 1'. How could I convert the user inputted 'A' into the number 1?
:

char cVal = 'A';
int iVal = cVal - 'A' + 1; // iVal becomes 1 at this point
• : char cVal = 'A';
: int iVal = cVal - 'A' + 1; // iVal becomes 1 at this point

I just wanted to point out that this is not guaranteed to work by the standard. '0'-'9' are guaranteed to have sequential character codes, but not 'A'-Z' or 'a'-'z'. In other words, you're allowed to assume that '0'+1 == '1', but not that 'A'+1 == 'B'. To put in context of your problem, you're allowed to assume that '3'-'1' == 2, but not that 'C'-'A'='B'.

And yes, there are real world machines where such an assumption would fail (though you be unlikely to ever work on one if your entire career is in the desktop PC universe). You could protect your code from silently breaking on such a machine by including something like this in your code:
[code=ffffff]
[color=a020f0] #if [/color][color=bb0000]'A'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'B'[/color][color=a020f0] || [/color][color=bb0000]'B'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'C'[/color][color=a020f0] || [/color][color=bb0000]'C'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'D'[/color][color=a020f0] || [/color][color=bb0000]'D'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'E'[/color][color=a020f0] || [/color]
[color=a020f0] [/color][color=bb0000]'E'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'F'[/color][color=a020f0] || [/color][color=bb0000]'F'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'G'[/color][color=a020f0] || [/color][color=bb0000]'G'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'H'[/color][color=a020f0] || [/color][color=bb0000]'H'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'I'[/color][color=a020f0] || [/color]
[color=a020f0] [/color][color=bb0000]'I'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'J'[/color][color=a020f0] || [/color][color=bb0000]'J'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'K'[/color][color=a020f0] || [/color][color=bb0000]'K'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'L'[/color][color=a020f0] || [/color][color=bb0000]'L'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'M'[/color][color=a020f0] || [/color]
[color=a020f0] [/color][color=bb0000]'M'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'N'[/color][color=a020f0] || [/color][color=bb0000]'N'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'O'[/color][color=a020f0] || [/color][color=bb0000]'O'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'P'[/color][color=a020f0] || [/color][color=bb0000]'P'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'Q'[/color][color=a020f0] || [/color]
[color=a020f0] [/color][color=bb0000]'Q'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'R'[/color][color=a020f0] || [/color][color=bb0000]'R'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'S'[/color][color=a020f0] || [/color][color=bb0000]'S'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'T'[/color][color=a020f0] || [/color][color=bb0000]'T'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'U'[/color][color=a020f0] || [/color]
[color=a020f0] [/color][color=bb0000]'U'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'V'[/color][color=a020f0] || [/color][color=bb0000]'V'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'W'[/color][color=a020f0] || [/color][color=bb0000]'W'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'X'[/color][color=a020f0] || [/color][color=bb0000]'X'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'Y'[/color][color=a020f0] || [/color]
[color=a020f0] [/color][color=bb0000]'Y'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'Z'[/color]
[color=a020f0] #error This code makes assumptions about the character set that aren't true[/color]
[color=a020f0] #endif[/color]
[/code]Or you could write a portable routine to convert the character code, something like this:

[code=ffffff]
[color=000080]int[/color] positionInAlphabet ([color=000080]char[/color] c) {
[color=000000][b]switch[/b][/color] (tolower(c)) {
[color=000000][b]case[/b][/color] [color=bb0000]'a'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]1[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'b'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]2[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'c'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]3[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'d'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]4[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'e'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]5[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'f'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]6[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'g'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]7[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'h'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]8[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'i'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]9[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'j'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]10[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'k'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]11[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'l'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]12[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'m'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]13[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'n'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]14[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'o'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]15[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'p'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]16[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'q'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]17[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'r'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]18[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'s'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]19[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'t'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]20[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'u'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]21[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'v'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]22[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'w'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]23[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'x'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]24[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'y'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]25[/color];
[color=000000][b]case[/b][/color] [color=bb0000]'z'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]26[/color];
[color=000000][b]default[/b][/color] [color=000000][b]return[/b][/color] [color=bb0000]0[/color];
}
}
[/code]

Or this:
[code=ffffff]
[color=000080]int[/color] positionInAlphabet ([color=000080]char[/color] c) {
[color=000080][b]static[/b][/color] [color=000080][b]const[/b][/color] [color=000080]char[/color]* alpha = [color=bb0000]"abcdefghijklmnopqrstuvwxyz"[/color];
[color=000080][b]const[/b][/color] [color=000080]char[/color]* offset = strchr(alpha, tolower(c));
[color=000000][b]return[/b][/color] offset ? (offset - alpha + [color=bb0000]1[/color]) : [color=bb0000]0[/color];
}
[/code]Or something else (I'm sure there's half a dozen ways to do this).

• : : char cVal = 'A';
: : int iVal = cVal - 'A' + 1; // iVal becomes 1 at this point
:
: I just wanted to point out that this is not guaranteed to work by the standard. '0'-'9' are guaranteed to have sequential character codes, but not 'A'-Z' or 'a'-'z'. In other words, you're allowed to assume that '0'+1 == '1', but not that 'A'+1 == 'B'. To put in context of your problem, you're allowed to assume that '3'-'1' == 2, but not that 'C'-'A'='B'.
:
: And yes, there are real world machines where such an assumption would fail (though you be unlikely to ever work on one if your entire career is in the desktop PC universe). You could protect your code from silently breaking on such a machine by including something like this in your code:
: [code=ffffff]
: [color=a020f0] #if [/color][color=bb0000]'A'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'B'[/color][color=a020f0] || [/color][color=bb0000]'B'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'C'[/color][color=a020f0] || [/color][color=bb0000]'C'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'D'[/color][color=a020f0] || [/color][color=bb0000]'D'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'E'[/color][color=a020f0] || [/color]
: [color=a020f0] [/color][color=bb0000]'E'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'F'[/color][color=a020f0] || [/color][color=bb0000]'F'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'G'[/color][color=a020f0] || [/color][color=bb0000]'G'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'H'[/color][color=a020f0] || [/color][color=bb0000]'H'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'I'[/color][color=a020f0] || [/color]
: [color=a020f0] [/color][color=bb0000]'I'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'J'[/color][color=a020f0] || [/color][color=bb0000]'J'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'K'[/color][color=a020f0] || [/color][color=bb0000]'K'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'L'[/color][color=a020f0] || [/color][color=bb0000]'L'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'M'[/color][color=a020f0] || [/color]
: [color=a020f0] [/color][color=bb0000]'M'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'N'[/color][color=a020f0] || [/color][color=bb0000]'N'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'O'[/color][color=a020f0] || [/color][color=bb0000]'O'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'P'[/color][color=a020f0] || [/color][color=bb0000]'P'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'Q'[/color][color=a020f0] || [/color]
: [color=a020f0] [/color][color=bb0000]'Q'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'R'[/color][color=a020f0] || [/color][color=bb0000]'R'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'S'[/color][color=a020f0] || [/color][color=bb0000]'S'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'T'[/color][color=a020f0] || [/color][color=bb0000]'T'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'U'[/color][color=a020f0] || [/color]
: [color=a020f0] [/color][color=bb0000]'U'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'V'[/color][color=a020f0] || [/color][color=bb0000]'V'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'W'[/color][color=a020f0] || [/color][color=bb0000]'W'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'X'[/color][color=a020f0] || [/color][color=bb0000]'X'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'Y'[/color][color=a020f0] || [/color]
: [color=a020f0] [/color][color=bb0000]'Y'[/color][color=a020f0] + [/color][color=bb0000]1[/color][color=a020f0] != [/color][color=bb0000]'Z'[/color]
: [color=a020f0] #error This code makes assumptions about the character set that aren't true[/color]
: [color=a020f0] #endif[/color]
: [/code]Or you could write a portable routine to convert the character code, something like this:
:
: [code=ffffff]
: [color=000080]int[/color] positionInAlphabet ([color=000080]char[/color] c) {
: [color=000000][b]switch[/b][/color] (tolower(c)) {
: [color=000000][b]case[/b][/color] [color=bb0000]'a'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]1[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'b'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]2[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'c'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]3[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'d'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]4[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'e'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]5[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'f'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]6[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'g'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]7[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'h'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]8[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'i'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]9[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'j'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]10[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'k'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]11[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'l'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]12[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'m'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]13[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'n'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]14[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'o'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]15[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'p'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]16[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'q'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]17[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'r'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]18[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'s'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]19[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'t'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]20[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'u'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]21[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'v'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]22[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'w'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]23[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'x'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]24[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'y'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]25[/color];
: [color=000000][b]case[/b][/color] [color=bb0000]'z'[/color]: [color=000000][b]return[/b][/color] [color=bb0000]26[/color];
: [color=000000][b]default[/b][/color] [color=000000][b]return[/b][/color] [color=bb0000]0[/color];
: }
: }
: [/code]
:
: Or this:
: [code=ffffff]
: [color=000080]int[/color] positionInAlphabet ([color=000080]char[/color] c) {
: [color=000080][b]static[/b][/color] [color=000080][b]const[/b][/color] [color=000080]char[/color]* alpha = [color=bb0000]"abcdefghijklmnopqrstuvwxyz"[/color];
: [color=000080][b]const[/b][/color] [color=000080]char[/color]* offset = strchr(alpha, tolower(c));
: [color=000000][b]return[/b][/color] offset ? (offset - alpha + [color=bb0000]1[/color]) : [color=bb0000]0[/color];
: }
: [/code]Or something else (I'm sure there's half a dozen ways to do this).
:
:
I have tried every suggestion and none of them work.
• : I have tried every suggestion and none of them work.
:

Just for grins, see if something like below is what you're after. Although it's hardly fool proof.[code]#include
#include

int main(void)
{
char ch, *input[] = { "A 1", "B 2", "C 3", "D 4" };
int i, coords[2] = { 0 };

for(i=0; i < 4; i++)
{
if((sscanf(input[i], "%c %d", &ch, &coords[1])) != 2)
printf("Unable to convert %s. Input was skipped.
", input[i]);
else
{
switch(toupper(ch))
{
case 'A': coords[0] = 1; break;
case 'B': coords[0] = 2; break;
case 'C': coords[0] = 3; break;
case 'D': coords[0] = 4; break;
default: coords[0] = coords[1] = -1; break;
}

printf("coords = (%2d,%2d)
", coords[0], coords[1]);
}
}

return (0);
}[/code]

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!