0

I'm trying to use switch case with char[2] in C, but it only supports integers.

int main() {
        char c[2] = "h";
        switch (c) {
                case "h":
                        printf("done!");
        }
        return 1;
}

For better understanding, what I'm trying to do is:

if "h" in ")($+#&@+)#"

Basically I want to make a condition that matches a character with a group of characters, but efficiently, the first thing that came to mind is to use switch case but it only supports int. But when I use (int)c which is recommended in other stackoverflow answers, it doesn't return the ascii value.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Does this answer your question? [How can I compare strings in C using a \`switch\` statement?](https://stackoverflow.com/questions/4014827/how-can-i-compare-strings-in-c-using-a-switch-statement) – user17732522 Feb 13 '22 at 22:18
  • 2
    Wait, you only want to `switch` on a single character? If so, you can just do that: `switch (c[0]) { case 'h': /*...*/`. But there are already standard library functions for searching substrings or characters in strings, so I am not sure what the purpose of the `switch` is. – user17732522 Feb 13 '22 at 22:20
  • no, let's say it's 50 characters, I wouldn't use it. –  Feb 13 '22 at 22:21
  • you mean "switch(c[0])" – Yvain Feb 13 '22 at 22:25
  • @user17732522 answering the second question, Thanks! why doesn't it work with double quotes? –  Feb 13 '22 at 22:26
  • @KapesoftFx Because `"h"` is a _string literal_ of type `char[2]` and `'h'` is a _character literal_ of type `int`. – user17732522 Feb 13 '22 at 22:29
  • @user17732522 Thanks! Finally, you had said that there are functions of the standard library that do exactly that, can you mention any function? Something like this will work: `'h' in '+$-h' // true` –  Feb 13 '22 at 22:36
  • @KapesoftFx `strchr` to search for characters and `strstr` to search for substrings. One of the answers below explains the use of `strchr`. – user17732522 Feb 14 '22 at 00:42

2 Answers2

3

Using the switch statement in this case does not make a sense.

Just use the standard function strchr declared in <string.h>.

#include <string.h>

//...

if ( strchr( c, 'h' ) != NULL ) puts( "done!" );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You can't compare arrays of characters (strings) using a switch statement directly, because switch statements only work with fundamental types; not arrays of fundamental types.

The reason using (int)c isn't returning the ASCII value is because you're casting char[]->int instead of char->int.

The difference between single and double quotes is an important one in C:

  • 'h' is of type char, and is equal to 104 (Lowercase 'h' in ASCII)
  • "h" is of type const char*, and points to 2 characters: h, & the null terminating character \0

To check if a character is present in a string, you can use the strchr function as mentioned in Vlad from Moscow's answer, or if you want to do it yourself using switch statements you could try this:

int main()
{
    char c[2] = "h";
    bool found = false;
    for (int i = 0; i < 2; ++i) {
        switch (c[i]) {
        case 'h': // compare c[i] == 'h'
            found = true;
            break;
        default:
            break;
        }
    }
    if (found)
        printf("Done!");
    return 0;
}
radj307
  • 439
  • 4
  • 11