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