I have to implement a function that calculates the hash h_N
of a string according to a formula h_0 = 1337
, h_n = h_n-1 * 5 + c_n
, where c_1
and c_n
are the strings characters for my homework.
I thought it would be a great idea to calculate it recursively but unfortunately I get a segmentation fault when trying to remove the last character of my given string.
uint64_t hashString(char *c)
{
if(strlen(c) == 1) {
return 1337 * 5 + (int) *c;
}
int lc = c[strlen(c) - 1];
c[strlen(c) - 1] = 0;
return hashString(c) * 5 + lc;
}
I even tried to use memmove
but it did nothing. What am I doing wrong?