Can you use switch statements with strings? If so a do I achieve that? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can you use switch statements with strings? If so a do I achieve that?

31st May 2017, 10:05 AM
Yehx H.
Yehx H. - avatar
2 Answers
+ 10
Switch statements only work with integers and characters for case. Still, there are ways around this. For example, you can use the first character of the string to denote what should be done. E.g. string str = "test"; switch (str[0]) { case 'a' : //something case 'g' : //something case 't' : //something }
31st May 2017, 10:09 AM
Hatsy Rei
Hatsy Rei - avatar
0
You can't do this leastways not in C++.
31st May 2017, 10:11 AM
Damian Tomalka
Damian Tomalka - avatar