0

//This is case insensitive as it should be

unsigned int hash(const char *word) { // TODO: Improve this hash function if ((strlen(word)) <4 ) { return (tolower(word[0]) - 'a'); } else { return (26 + ((676 * (tolower(word[0]) - 'a')) + (26 * (tolower(word[1]) - 'a')) + (tolower(word[2] - 'a')))); } } 

// This is not case insensitive

unsigned int hash(const char *word) { // TODO: Improve this hash function if ((strlen(word)) <3 ) { return (tolower(word[0]) - 'a'); } // else if(strlen(word)==3){ // return (26 + ((26 * (tolower(word[0]) - 'a')) + 26*(tolower(word[1]) - 'a'))); // } else { return (26 + ((676 * (tolower(word[0]) - 'a')) + (26 * (tolower(word[1]) - 'a')) + (tolower(word[2] - 'a')))); } } 

//Only difference is <3 and <4 in if condition

2
  • Is there a question in there somewhere? Commented Sep 5, 2024 at 13:53
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Sep 6, 2024 at 1:05

1 Answer 1

1

actually problem is in return of else part, i figured it out through printing. its 3rd part is (tolower(word[2] - 'a')) but it should be (tolower(word[2]) - 'a') that shows how important () are

1
  • yeah details matter... think about the difference and why it matters one calls tolower with argument of (word[2]-'a') while the other calls tolower with word[2] and then subtracts a from the result.. Very different things. Commented Sep 6, 2024 at 21:51

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.