Forums Programming Assignment Help What is hashing in C

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #15372
    nammy
    Participant

    What is hashing in C?

    #15378
    juwanine
    Participant

    Answer: The process of mapping strings to integers is known as hashing in C. A “hash function” maps a string to a bounded number which can more easily be used as an index in an array. An extremely simple hash function for strings to add up the values of all the characters:
    unsigned hash(char *str)
    {
    unsigned int h = 0;
    while(*str != ”)
    h += *str++;
    return h % NBUCKETS;
    }
    A somewhat better hash function is
    unsigned hash(char *str)
    {
    unsigned int h = 0;
    while(*str != ”)
    h = (256 * h + *str++) % NBUCKETS;
    return h;
    }

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.