data-structures · intermediate · ~15 min

djb2 string hash

Turn a string into a reproducible hash value.

Challenge

Implement the classic djb2 hash: unsigned long djb2(const char *s) starting at 5381 and, for each byte, computing hash = hash * 33 + byte.

Starter code

unsigned long djb2(const char *s) {
    /* TODO */
    return 5381;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.