1. ## Hashing a string

I need to write a hash function where I take the ascii values of every letter in a string and write them to a string, after which, I take said string and divide it into sub-sections consisting of 3 integers, get the sum of those integers, and modulo by a given prime number.

For example, if the key were "word", then the ascii values would be:
119 111 114 100
and the string to which I would write them would look like:
"119111114100"
Then get the first 3 sections of 3
119 111 114
Use sscanf to initialize three integers (a, b, c)
a = 119
b = 111
c = 114
Get the sum
sum = a + b + c
and modulo by a predefined prime number

Here's what I thought of:
Code:
```int hashString (void *pKey)
{
int i, sum, a, b, c;
char *key, *values, temp[4];
key = (char*)pKey;
values = (char*)calloc(3 * strlen(key) + 1, sizeof(char));
for(i = 0; i < strlen(key); i++)
{
sprintf(temp, "%d", (int)key[i]);
printf("%s is the ascii value\n", temp);
if(strlen(values) == 0){
sprintf(values, "%d", temp);
}
else{
strcat(values, temp);
}
}
sscanf(values, "%3d%3d%3d", &a, &b, &c);
printf("%s is the total string\n", values);
//free(values);
sum = a + b + c;
return sum % SIZE;
}```
Ideally I would want to be able to realloc "values" each time it concatenates to the string so that I don't end up wasting too much memory.
Another thing, whenever I try to free values, my program crashes.

Any and all help is appreaciated

2. For example, if the key were "word", then the ascii values would be:
119 111 114 100
and the string to which I would write them would look like:
"119111114100"
Then get the first 3 sections of 3
119 111 114
Use sscanf to initialize three integers (a, b, c)
a = 119
b = 111
c = 114
Get the sum
sum = a + b + c
and modulo by a predefined prime number
Why not do
sum = key[0] + key[1] + key[2]
and cut out the pointless sprintf / sscanf steps.

3. Well it's part of the assignment:

4. Your example in post#3 translates 'a' as 61 (the ascii value in hex), then 'n' as 110 (ascii in decimal), then 'a' as 64 (hex again). Is that what you're supposed to do?

What are you supposed to do if you have more than 9 digits in values?

If you're only supposed to deal with the first 9 chars then it hardly seems necessary to realloc values. You could simply declare a local char array of, say, 15 chars and end the loop when it has at least 9 chars in it.

It's not obvious why your program would crash when you free values, but that kind of thing can happen if you've messed up the heap somehow somewhere.

Why is pKey void*? Why not char*?

"temp" is a bad variable name if you can think of something better. How about "ascii"?

Get rid of the sprintf into values (and its controlling if); just use the strcat (values is initially all zeros so strcat will copy the first string to the beginning anyway).

5. Originally Posted by oogabooga
What are you supposed to do if you have more than 9 digits in values?
Also, what do you do if you only have six digits?

"cab" = 676566 = 676566???

Quzah.