Question: Hello, can someone please explain this code to me line by line please? Thank you. Also if possible show sample output. unsigned long hashDJB2(char *str)

Hello, can someone please explain this code to me line by line please? Thank you. Also if possible show sample output.

unsigned long hashDJB2(char *str)

{

unsigned long hash = 5381;

int c;

while ((c = *str++))

hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

return hash;

}

unsigned long hashKR(char *str)

{

unsigned int hash = 0;

int c;

while ((c = *str++))

hash += c;

return hash;

}

int main(int argc, char *argv[])

{

char *buffer;

int bufferlen = MAXSTRING;

unsigned long newHash;

unsigned long newKRhash;

buffer = malloc(MAXSTRING);

if (buffer == NULL)

{

printf("Bad malloc bye! ");

return 1;

}

while (fgets(buffer, bufferlen, stdin))

{

// printf("data in:%s", buffer);

if (ferror(stdin))

{

printf("Error on stdin ");

break;

}

/*

* Hash 'em up - commences!

*/

newHash = hashDJB2(buffer);

newKRhash = hashKR(buffer);

printf("hash:%016lx:%016lx:%s", newHash, newKRhash, buffer);

}

printf(" Done! ");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!