Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, can someone please explain this code to me line by line please? Thank you

ID: 3713091 • Letter: H

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)

{

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;

}

Explanation / Answer

Solution:

Note: The code is commented line by line for explanation:

unsigned long hashDJB2(char *str) //A method names as hashDJB2

{

unsigned long hash = 5381; //hash variable is declared and initialized with 5381

int c; //variable declaration

while ((c = *str++)) //iterate forever and assign str values to c one by one

hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ //hash ios calculated by shifting 5 bits to the left and adding that value with the hash and then with the c

return hash; //hash will be returned from here

}

unsigned long hashKR(char *str) //method hashKR

{

unsigned int hash = 0; //variable declariations

int c;

while ((c = *str++)) //iterate forever and assign str values to c one by one

hash += c; //hash= hash+c

return hash; return the hash

}

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

{

char *buffer; variable declaration

int bufferlen = MAXSTRING;

unsigned long newHash;

unsigned long newKRhash;

  

buffer = malloc(MAXSTRING); //memory allocation to the buffer

if (buffer == NULL) //if the buffer is null then

{

printf("Bad malloc bye! "); //display ba malloc

return 1;

}

while (fgets(buffer, bufferlen, stdin)) //else start reading values from the user

{

//   printf("data in:%s", buffer); //print the buffer item which is read from the user

if (ferror(stdin)) //if error

      {

      printf("Error on stdin ");

      break;

      }

   /*

   * Hash 'em up - commences!

   */

   newHash = hashDJB2(buffer); //calling hash function hashDJB2

   newKRhash = hashKR(buffer);  //calling hash function hashKR

   printf("hash:%016lx:%016lx:%s", newHash, newKRhash, buffer); display returned values hashes

  

}

printf(" Done! ");

return 0;

}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote