No, your logic is not correct, well, at least not for all the character sets that C++ can use. In ASCII, the letters of the alphabet are in contiguous blocks, so something like
(s[i]<'A'||s[i]>'Z')
works just fine. The issue with that is that ASCII isn't the only character set C++ supports. The most common example to counterpoint ASCII is EBCDIC which has the characters {
, }
, and
in between A
and Z
.
One thing that is guaranteed though is that 0
through 9
are contiguous in all character sets that C++ supports so it's always legal to text if a character is a number using
if (char_var >= '0' && char_var <= '9')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…