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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started