user3869795 user3869795 - 1 year ago 110
C++ Question

Specify a range of ASCII lowercase chars in C++

I am writing a program that takes a char and compares it to see if it's in a range of certain chars. For instance, if the char I get is an

I go to state 3, if its a - m or o - z I go to state 4. I'm new to C++ so I'm still learning.

Can I say something like:

char c = file.next_char();
if (c in 'a'...'m', 'o'...'z')
state = 3;
} else {
state = 4;

Answer Source

There is no such syntax in C++. The options are:

  1. Use a switch statement, when the list of values is generally not contiguous, or

  2. Convert the list of explicit character values into contiguous ranges into equivalent boolean expressions. As you know, alphabetic characters consist of a contiguous range of octets in ASCII, so your pseudo-code is equivalent to:

    if ( (c >= 'a' && c <= 'm')
         (c >= 'o' && c <= 'z'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download