0

I'm using || so that if the user types in a Y or y it equals the same. Example: (option=='y' || option =='Y') I know there's a single function to get rid of this so that the user types in Y or y it equals the same.

What would be the best way to do this?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Gus
  • 69
  • 5

4 Answers4

2

You could try to use tolower((unsigned char)option) == 'y' as described on https://www.tutorialspoint.com/c_standard_library/c_function_tolower.htm (you need to add #include <ctype.h>).

Or use a switch statement with fallthrou cases for 'Y' and 'y'.

I suppose if you often have (option=='y' || option =='Y') I suppose the best way is to put it into a separate function in your favourite way and call the function ;)

MrTux
  • 32,350
  • 30
  • 109
  • 146
2

Assuming option is of type char, you can replace

if (option == 'y' || option == 'Y')

by

if (tolower((unsigned char)option) == 'y')

The cast to unsigned char is necessary. tolower takes an argument of type int whose value must be either within the range of unsigned char or equal to EOF (which is typically -1). If plain char happens to be signed (which is common) and if the value of option happens to be negative (which could happen if the input is unusual), then tolower(option) would have undefined behavior.

(Yes, the need for a cast is annoying and counterintuitive. Yes, tolower() should accept any argument within the range of char. Yes, you still have to deal with it.)

Other possibilities:

option = tolower((unsigned char)option);
if (option == 'y') ...

or

switch (option) {
    case 'y': case 'Y': ...
    ...
}

The switch solution still tests against both 'y' and 'Y', but it's less verbose; you don't have to mention option more than once. And of course you can also switch on tolower((unsigned char)option).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1
  1. A fast, albeit memory wasteful approach, is to use a look-up table:

    #include <limits.h>
    #include <stdbool.h>
    
    // Initialize all values to false, except for two.
    // Note 'y', 'Y' have positive values in all encodings.
    static const bool IsYory[UCHAR_MAX + 1u] = {
        ['y'] = 1,
        ['Y'] = 1,
        };
    
    char option = foo();
    if (IsYory[(unsigned char) option]) {
      bar();
    }
    
  2. A branch-less method

    if (((option - 'y')*(option - 'Y')) == 0) {
    
  3. If the two letters differ only by 1 bit, like 'y' and 'Y' do with ASCII and even EBCDIC encoding,

    if ((option | ('y' ^ 'Y')) == ('y' | 'Y'))  {
    
  4. Best to simply use toupper(). Prefer toupper() over tolower() for more character sets, beyond 'A-Z,a-z' collapse to upper characters better than to lower. For A-Z, it makes no difference.

    if (toupper(option) == `Y`)
    
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Make all that the user inputs either lowercase or uppercase using the tolower() or toupper() functions, so you won't have a problem. More info:Description

Satyam Raj
  • 61
  • 1
  • 11