Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:58:09+00:00 2026-06-10T17:58:09+00:00

Our system needs to accept user input from a terminal and match against a

  • 0

Our system needs to accept user input from a terminal and match against a few known keyword strings (maybe 10).

We don’t have the space/computrons to do regexp etc., code needs to be tiny & quick.

Now, the nasty way to do this is:

   // str is null-terminated, assume we know it's safe/sane here
   if(!strncmp(str,"hello",5)
   {
      do_hello();
   }
   else if(!strncmp(str,"world",5)
   {
      do_world();
   }
   else
   {
      meh(); // Wasn't a match
   }

So, after a bit of googling & reading I’m being convinced that a nicer way is to pre-compute the hash of the various matches as an int, and then just use a case statement:

// Assume hash() stops at NULL
switch(hash(str))
{
   case HASH_OF_HELLO:
      do_hello();
      break;

   case HASH_OF_WORLD:
      do_world();
      break;

   default:
      meh();
      break;
}

We can compute the *HASH_OF_match* at compile time. This seems potentially a faster / more elegant way to pick a string from a relatively small set.

So – does this seem reasonable? / Is there a glaring problem with doing this? / Anyone got a more elegant way of doing it?

As a footnote, this is the nicest looking hash algorithm I’ve seen this afternoon ;), credited to dan bernstein, it looks up to the job at hand.

unsigned int
get_hash(const char* s)
{
    unsigned int hash = 0;
    int c;

    while((c = *s++))
    {
        // hash = hash * 33 ^ c 
        hash = ((hash << 5) + hash) ^ c;
    }

    return hash;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:58:10+00:00Added an answer on June 10, 2026 at 5:58 pm

    The problem with hashing is that an arbitrary string entered by the user may generate the same hash as one of your matches and you’ll execute the wrong stuff. For a search set as small as 10 I’d just stick to the if-else approach. Or use a string array and function pointer array (assuming all functions have the same signature) to select the function to execute.

    char const *matches[10] = {"first", "second", ..., "tenth"};
    void (*fn[10])(void) = {&do_first, &do_second, ..., &do_tenth};
    
    for( i = 0; i < 10; ++i ) {
      if( strcmp( str, matches[i] ) == 0 ) {
        (*fn[i])();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a system that needs to be able to accept input from both
Our invoicing system needs to print OMR (optical mark recognition) symbol on each page
I have statistics for every user in our system. These statistics need to be
I am trying to transform an XML output from our accounting system into an
Our business is transitioning from an old asp system to a newer ASP.NET system,
I am creating a booking system for some student cabins. The user control needs
Our system complexity has risen to the point that we need to make permission
In our system, we need to pass along a pair of IDs for each
As a part of our application I need to write simple workflow system which
Our system uses an Entity Framework based data layer. For the recent months, we've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.