In C language, is it possible to pass string as a case condition in switch case?
If possible, can AI (Artificial Intelligence) be achieved?
Question posted in Artificial Intelligence
ChatGBT is becoming a world-wide phenomena, try it out here.
ChatGBT is becoming a world-wide phenomena, try it out here.
2
Answers
The short answer is that a switch in C only accepts integer values, or something easy to cast to an integer (like a character).
There are workarounds tough, like using a hash function or using a series of “if”.
Concerning Artificial Intelligence, this is a vast field, that cannot be addressed with a simple switch. At least for a good result. You can have a look at expert systems as a start.
No – the
switch
statement only operates on integer values;case
labels must be constant integral expressions. You’d have to map the string onto an integer value, either by using a hash function or a lookup table or something.Will case or whitespace matter? In other words, should
"THIS IS A TEST"
and"This is a test"
and"tHiS iS a TeSt"
all give the same result? If so, then yourmapToInteger
function (or whatever you decide to call it) will have to take all that into account.This is the programming equivalent of asking, “if I can use a hammer to nail two pieces of wood together, can I build an apartment complex?” Yeah, sure, but you need to do a bunch of other stuff as well.