Though your code works, it doesn't handle fall-through and won't be convertible to a jump table.
Since the strings must be constants anyhow, it would make sense to just alphabetize them and use a binary search (O(log n)) instead of an else-if tree (O(n))
#define MAGIC_DATA \
MAP(abcdef) \
MAP(ghijkl) \
MAP(nopqrs) \
MAP(tuvwxy)
#define MAP(x) ENUM_##x,
enum myenums { MAGIC_DATA };
#undef MAP
#define MAP(x) #x,
const char * mystrings[] = { MAGIC_DATA };
#undef MAP
#undef MAGIC_DATA
//different than standard bsearch - not void* and returns index
long my_bsearch(const char *key, const char **base, size_t nmemb){
for (long bot=0, top=nmemb, i=top/2; bot<=top ; i=(bot+top)/2){
int cmp=strcmp(key,base[i]);
if (! cmp) return i; //match found
else if (cmp>0) bot=i+1;
else top=i-1;
}
return -1;
}
Now you can use them in a switch case like this:
int main(int argc, char **argv) {
switch (my_bsearch(argv[1], mystrings, sizeof(mystrings)/sizeof(mystrings[0]))){
case ENUM_abcdef : printf ("A1!\n"); break;
case ENUM_ghijkl : printf ("B1!\n"); break;
case ENUM_nopqrs : //fallthrough
case ENUM_tuvwxy : printf ("D1!\n"); break;
default : printf ("*!\n");
}
//or you can vectorize the result
long i = my_bsearch(argv[1], mystrings, sizeof(mystrings)/sizeof(mystrings[0]));
printf("%c1!\n",(i==-1)?'*':"ABDD"[i]);
return 0;
}
This example only used strings without spaces, if you have spaces in the strings its only slightly different
#define MAGIC_DATA \
MAP(abcdef, "a b c d e f", extra) \
MAP(ghijkl, "g h i h k l", extra, more) \
MAP(nopqrs, "m n o p q r", more) \
MAP(tuvwxy, "s t u v w x", lots, of, random, things)
#define MAP(x,y,...) ENUM_##x,
enum myenums { MAGIC_DATA };
#undef MAP
#define MAP(x,y,...) y,
const char * mystrings[] = { MAGIC_DATA };
#undef MAP
//do similar stuff with __VA_ARGS__ here:
#undef MAGIC_DATA