So using if-else ladder and strcmp are inevitable.And here are simplest macros to simulate the switch-case for strings.Here is reverse string SWITCH, where in you can use a variable (rather than a constant) in CASE clause:If it is a 2 byte string you can do something like in this concrete example where I switch on ISO639-2 language codes.LANIDX_* being constant integers used to index in arrays.Assuming little endianness and sizeof(char) == 1, you could do that (something like this was suggested by MikeBrom)....this calls a function that you have set by string key (one function per case):Use a pre-existing hashmap/table/dictionary implementation such as khash, return that pointer to a function inside of Hi this is the easy and fast way if you have this case :for example : Convert them to int and them switch based on ASCII code.yes !! By clicking “Post Your Answer”, you agree to our To subscribe to this RSS feed, copy and paste this URL into your RSS reader. but is this only the way for doing this stuff?? Lệnh switch case trong C/C++ - Học C/C++ cơ bản và nâng cao cho người mới học từ Ngôn ngữ C/C++ hướng đối tượng, Cú pháp cơ bản, Biến, Hàm, Kiểu dữ liệu, Tính kế thừa, Nạp chồng, Tính đa hình, Tính bao đóng, Xử lý ngoại lệ, Template, Overriding, Toán tử, … @Onion: You'll note that MikeBrom does not currently have the reputation to comment on posts other than his own and answers to his own questions. I understood and fixed it!! In case you have to use a given enumeration where an enumerator with value zero is defined, you should call std::map::find() before the switch statement to check if the string value is valid. By using our site, you acknowledge that you have read and understand our How, given the string only, would you pick out an integer to go with it? A simple program can be made to pass char *'s through the hash function and output their results. At least it's not performing equal for each value.

The original downvoter is long gone. That's mostly a word of caution; I'm certainly not about to down-vote you for this. the same thing with the 2nd menu and the 3rd menu.but the Options are diferent A switch statement allows a variable to be tested for equality against a list of values. Good to know. 2. a string.
tq What you've shown is nicely presented and a good idea, but … but it isn't all that distinctively different from some of the other answers — there are several that use minor variants on this idea. C# string with switch case statement. @tristopia You are right of course(as right as one can be after attempting to do something like this for real). @RuchiM.Mewada: There are others but they are putting up an unnecessary fight with the language. @ArcaneEngineer Um... isn't that the exact problem the question is trying to solve? @ebyrob I meant anything comparable in a quick op, such as 2 64-bit Much nicer to use an enum instead of a set of #defines for the keys, but otherwise about the best you can do.the incrementing is incorrect. avoid the so-called "If you mean, how to write something similar to this:Then the canonical solution in C is to use an if-else ladder:If you have many cases and do not want to write a ton of You just have to make sure your hash function has no collisions inside the set of possible values for the string.There is no way to do this in C. There are a lot of different approaches. @harper it's not the case for x86.Niklas didn't ask for x86. lookuptable + i*sizeof(t_symstruct) is not equal to lookuptable[i]. I don't think this is too much of an issue; however, since the switch statement operates on fixed values regardless. Lacks (probably by choice) defensive coding. Featured on Meta 4. an integral value, such as an int or a long. @eri0o If you thought it was decent, why not upvote it? Neither in switch controlling expression nor in Keep in mind this works by pure, unadulterated magic tricks, and is not suitable for large collections of text values.Also, the validity of the match is entirely dependent on the degree to which you pre-process the user’s input. And since you mentioned the big endian case, you don't address exclusively the x86 environment. @Niklas: This is important information for your question. The syntax for a switch statement in C programming language is as follows −

"use a switch or if/else ladder" Or maybe you mean something very short like 4 characters? is this taking * as an arithmetic multi oprtr or dereference optr??" Making the macro support mixed endianness, or a function is left as an exercise for the reader.It is standard C, but not portable. Submitted by IncludeHelp, on March 17, 2019 . site design / logo © 2020 Stack Exchange Inc; user contributions licensed under Featured on Meta Where developers & technologists share private knowledge with coworkersProgramming & related technical career opportunitiesPlease reformulate so that the question makes clear what you are actually trying to do (or asking).The OP probably wants to use a string as the paramater of a switch statement.
-- Does such a hash function exists for the alphabet If you use fixed length string keys, you can convert them each into unique integers; no collisions possible. Means to compare character by character until an unmatch is found. i've tried this and its worked!! We might only want to simulate the switch-case for string, but can never replace if-else ladder. In C, a character literal is treated as int type where as in C++, a character literal is treated as char type" but I wasn't aware of that difference, just looked that up. The Overflow Blog Important points: Switching on strings can be more costly in term of execution than switching on primitive data types. Typically the simplest is to define a set of constants that represent your strings and do a look up by string on to get the constant:There are, of course, more efficient ways to do this. Its syntax is:In C# 6 and earlier, the match expression must be an expression that returns a value of the following types: 1. a char. The switch is not made on the string itself but on the numeric value associated to it by the std::map.