How to Create a replacement for the switch statement in C++ that also works for strings | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to Create a replacement for the switch statement in C++ that also works for strings

I need a solution

30th May 2017, 7:06 AM
Kibrom
Kibrom - avatar
5 Answers
+ 7
Switch also works for characters, and strings are character arrays. There's probably a way to make things work, but it will depend on the case of your program and the strings you want to evaluate.
30th May 2017, 11:55 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Maybe you can try marking each string with a unique number, and then check for the number using switch. That number may be : 0) Sum of All ASCII Codes of the characters in the string. 1) Sum of First Char's ASCII Code + Length of the string. etc,etc...
30th May 2017, 12:20 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
convert the string to a number maybe?
30th May 2017, 8:05 AM
jay
jay - avatar
+ 3
@Hatsy Rei Yes but characters are just numbers. It wouldnt make sense to go through the trouble instead of just using a simple if statement
30th May 2017, 4:29 PM
aklex
aklex - avatar
+ 2
You have to use if statements. Switches are only for numbers, not entire strings
30th May 2017, 7:25 AM
aklex
aklex - avatar