and told that I should convert the chars to int
Probably you didn't comprehend what he told you. In a computer "characters" are already numbers. There aren't special memory cells that have the shape of the character A.
You can:
char ch = 'A';
int num = ch;
or you can:
char ch1 = 'A';
char ch2 = 'B';
if (ch1 < ch2)
{
}
and for example (but it isn't needed for what you want to do):
char ch1 = 'A';
char ch2 = (char)(ch1 + 1); // 'B'
(note the final casting: when you do math operations on a char
, it is implicitly converted to an int
)
and so on.
Note that the opposite isn't true:
char ch = 'A';
int num = ch;
char ch2 = num; // COMPILATION ERROR
you need a cast:
char ch2 = (char)num;
(technically the correct definition is that there is an implicit cast conversion from char
to int
(used in the first and third examples) and there is an explicit cast conversion from int
to char
(used in the last example).
(technically [2] char
s should be sorted through the use of a collation (that is a special ordering for strings that knows that e < è < f
) but this is something a little overboard for a school exercise about bubble sorting)