switch on strings

F

Francine.Neary

Suggestion: do a web search for "perfect hash".
20.17: Is there a way to switch on strings?
A: Not directly. Sometimes, it's appropriate to use a separate
function to map strings to integer codes, and then switch on
those. Otherwise, of course, you can fall back on strcmp()

[snip]

For example....

#include <stdarg.h>
#include <stdlib.h>

/*
** string compare string Match to list of strings starting with Model
** return ordinal of successful match (>=0) or -1 on no match
*/

int WhichString(char *Match, ...)
{
va_list arg;
int ordinal = 0;
char *item;

va_start(arg,Match);
for(item=va_arg(arg, char *);item != NULL; item=va_arg(arg, char *))
{
if (strcmp(Match,item) == 0)
break;
else
++ordinal;
}
va_end(arg);

return (item != NULL ? ordinal : -1 );

}

And used like...

#include <stdio.h>

int WhichString(char *model, ...);

int main(int argc, char **argv)
{
enum {left,right,forward,back,up,down,halt} cmd;

while (--argc)
switch (cmd = WhichString(*++argv,
"left",
"right",
"forward",
"back",
"up",
"down",
"halt",
NULL))
{
case left: /* "left" */
puts("<--");
break;
case right: /* "right" */
puts("-->");
break;
case forward: /* "forward" */
puts(" <>");
break;
case back: /* "back" */
puts(" ><");
break;
case up: /* "up" */
puts(" ^\n |");
break;
case down: /* "down" */
puts(" |\n v");
break;
case halt: /* "halt" */
puts(" @\n !");
break;
case -1:
default:
puts("*** Unknown ***");
}

return 0;

}

Ouch! All those strcmps! Why not a simple hash function?
 
L

Lew Pitcher

how to implement a switch case for stringse...switch should happen
depending on the strings...
Suggestion: do a web search for "perfect hash".
From the C FAQ:
20.17: Is there a way to switch on strings?
A: Not directly. Sometimes, it's appropriate to use a separate
function to map strings to integer codes, and then switch on
those. Otherwise, of course, you can fall back on strcmp()

For example....
#include <stdarg.h>
#include <stdlib.h>
/*
** string compare string Match to list of strings starting with Model
** return ordinal of successful match (>=0) or -1 on no match
*/
int WhichString(char *Match, ...)
{
va_list arg;
int ordinal = 0;
char *item;
va_start(arg,Match);
for(item=va_arg(arg, char *);item != NULL; item=va_arg(arg, char *))
{
if (strcmp(Match,item) == 0)
break;
else
++ordinal;
}
va_end(arg);
return (item != NULL ? ordinal : -1 );
[snip]
Ouch! All those strcmps! Why not a simple hash function?

Sure, why not? Care to show an example of how a simple hash function
would be better?
 
U

user923005

how to implement a switch case for stringse...switch should happen
depending on the strings...
Suggestion: do a web search for "perfect hash".
From the C FAQ:
20.17: Is there a way to switch on strings?
A: Not directly. Sometimes, it's appropriate to use a separate
function to map strings to integer codes, and then switch on
those. Otherwise, of course, you can fall back on strcmp()
[snip]
For example....
#include <stdarg.h>
#include <stdlib.h>
/*
** string compare string Match to list of strings starting with Model
** return ordinal of successful match (>=0) or -1 on no match
*/
int WhichString(char *Match, ...)
{
va_list arg;
int ordinal = 0;
char *item;
va_start(arg,Match);
for(item=va_arg(arg, char *);item != NULL; item=va_arg(arg, char *))
{
if (strcmp(Match,item) == 0)
break;
else
++ordinal;
}
va_end(arg);
return (item != NULL ? ordinal : -1 );
}
[snip]
Ouch! All those strcmps! Why not a simple hash function?

Sure, why not? Care to show an example of how a simple hash function
would be better?

In this particular case, a hash function is not needed. Just switch
on the first letter.
 
F

Flash Gordon

user923005 wrote, On 01/10/07 20:45:
In this particular case, a hash function is not needed. Just switch
on the first letter.

A very simple hash function which just takes the first letter as the has
value ;-)

Of course, that could give lots of collisions if the strings are changed
or added to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top