can I do case-switch for string?

meaning I have 4 words I want do with them something
ok
bad
yes
no

I know how to read and compare them like this

void CmpString ()
{
  if (strcmp (printChars, key1) == 0 )
  {
    l
    Serial.println("Key1");
    }
else if (strcmp (printChars, key2) == 0 )
  {
Serial.println("Key2");
}

}

can do this in the case-switch ?
something like this

  switch (printChars) 
  {
    case 'OK':
    Serial.println("ok");
    break;
    case 'good':
    Serial.println("good");
    break;
    default;
    }

Thanks,

1 Like

No.
Single quotes just define a character not a string anyway.

As far as I am aware you cannot switch on strings. Only integers.

You could use an enum and switch on that:

enum state {
    OK, 
    BAD,
    YES,
    NO
};

int currentState = OK;

void setup() {
    Serial.begin(9600);
}

void loop() {
  switch(currentState) {
    case 0:
        Serial.println("Something OK");
        break;
    case 1:
        Serial.println("Uh-oh!");
        break;
    case 2:
        Serial.println("YES!");
        break;
    case 3:
        Serial.println("Nooooooo!");
        break;
  }
}

An enum, or enumeration, is a list of items. Typically it starts from 0, although you can do this:

enum state {
    OK = 1,
    BAD,
    YES,
    NO
};

To force it to start from another integer.

I have quickly verified the above code in the Arduino IDE, but not actually run it.

1 Like

This is a simple one, you could do switch on the first char as these are unique

switch(tolower(answer[0]))
{
  case 'y': // yes
    ...
    break;  
  case 'n': // no
    ...
    break;  
  case 'o': // ok
    ...
    break;  
  case 'b': // bad
    ...
    break;  
  default:  // ignore the rest
}
1 Like

My test programs at work use regular if-statements for things like that.

A couple of other things we do...

Any characters that are not typed-in as lower case are converted to lower case so the input is not case-sensitive.

There is usually a default, so the user can just hit the ENTER for "Yes", or "OK", or whatever is most the common input/answer. i.e. When there are only two options, such as "Yes" or "No", anything other than "No" is treated as "Yes".

Sometimes we just check the first character (or the first two characters, etc.). That allows the user to just key-in "Y" or "N" or it allows for minor typos. It could be programmed so anything that starts with an upper or lower case "N" is treated as a "No", and anything else (or just an ENTER) is treated as a"Yes" (or vice versa).

But, in some applications you might not want to "guess" what the user meant and you might want to require complete and accurate typing/spelling.

Thanks !
I didn't know I can only"use" the first latter and then do switch
(of course only if the first char is unique )

Thanks,

1 Like

david1234:
Thanks !
I didn't know I can only"use" the first latter and then do switch
(of course only if the first char is unique )

Thanks,

if the first char is not unique you could find another one, or apply a hash function on the string which gives an unique int.

check this page for inspiration - Hashing Tutorial: Section 2.4 - Hash Functions for Strings

1 Like

Thanks ,
I can work for now with unique char

1 Like