C Program To Implement Dictionary Using Hashing Passwords
Implementation of Data Structure in C. Practical Implementation of Data Structure and Algorithms. MENU: 1.Create 2.Search for a value 3.Delete an value Enter your choice:1 Enter the number of elements to be inserted:3 Enter the elements to be inserted:12 23 56 Enter y to Continue:y MENU: 1.Create 2.Search for a value 3.Delete an value Enter your choice:2 Enter the element to be searched:23 Search key is found!!
I thought of a simple way to hash a string. By taking the ASCII decimal value of each character, multiplying it by 10, and adding all of the values computed together for each character in a string. Is there a name for this algorithm? I highly doubt I was the first one to think of this.
Compiled with gcc -Wall -Wextra -Werror -std=c99 string.c -o string
2 Answers
$begingroup$Don't check for NULL pointer argument. The function should expect a valid null-terminated string, it's responsibility of the caller to ensure correct argument.
You don't need to know the string length. Check for null-terminator right in the hash loop.
It's possible to write it shorter and cleaner.
It's not quite clear what do you mean by 'ASCII decimal value'. Are you referring to this expression in your code:
c - '0'
? Well, suppose at some momentc 'Z'
, so this expression amounts to'Z' - '0'
. If we substitute ASCII codes for these characters, then we get90 - 48
, this is equal to 42 which is ASCII code for'*'
character. So you have transformed'Z'
into'*'
. Is this somehow supposed to improve the quality of your hash function? I'm in doubt.Are you aware that for the same expression
c - '0'
for a number of possiblec
values (e.g.' '
,'!'
, and anything with ASCII value less than 48) you will get a negative result and when you add it to thehash
it will be sign-extended and converted to a huge unsigned value, something like0xffffffffffffffxx
?If you are looking for a short and simple hash function then perhaps either of these might work for you.
I have only a few comments about your code, otherwise, it looks good. The string hashing algo you've devised should have an alright distribution and it is cheap to compute, though the constant 10
is probably not ideal (check the link at the end).
I don't see a need for reinventing the wheel here. You should use
strlen()
to compute the length of strings. It will more than likely be a lot better optimized than your customstringLength()
.However, you don't need to compute the string's length beforehand.
getHash()
can be optimized by using the null terminator in the string itself to infer its length. No need to do a pre-pass just to compute the length:Two minor details: In C, you should add
void
to the parameter list of functions that take no arguments, somain
should beint main(void)
. Also, you don't need to explicitlyreturn 0
at the end ofmain
. This function is treated specially by the compiler. If there's no explicit return, areturn 0
is added at the end ofmain
by default.
C Program To Implement Dictionary Using Hashing Passwords List
If you are interested in knowing more about hash functions and algorithms, I recommend reading this article.
glampertglampert