How can I represent a string as an integer in C for a hash table?

  • Thread starter Thread starter unknown_2
  • Start date Start date
  • Tags Tags
    Integers Strings
Click For Summary
SUMMARY

This discussion focuses on representing a string as an integer for a hash table in C using the division method. The key approach involves utilizing the ASCII values of characters in the string. For smaller datasets, the ASCII value of the first character suffices, while for larger datasets, a combination of ASCII values from multiple characters is recommended. The suggestion includes adjusting ASCII values by subtracting 32 to fit them into a manageable range.

PREREQUISITES
  • Understanding of C programming language and its data types
  • Familiarity with hash table concepts and collision resolution
  • Knowledge of ASCII values and character encoding
  • Basic understanding of the division method for hashing
NEXT STEPS
  • Research how to implement hash tables in C using the division method
  • Learn about different hashing techniques and their performance implications
  • Explore character encoding and its impact on hash functions
  • Study collision resolution strategies for hash tables in C
USEFUL FOR

C programmers, software developers working with data structures, and anyone interested in implementing efficient hash tables.

unknown_2
Messages
28
Reaction score
0
hey, I've hit a bump in the road here. I'm trying to build code for a hash table out of my textbooks pseudo code, and I'm not quite sure how I'd represent a string of characters such as "thisISastring", as an integer for my key variable. I'm using division method if that helps at all. I know the function atoi(), but that's only for number strings right?

thanks,
 
Technology news on Phys.org
First, you probably want to know the size of your hash. If you need in the ballpark of 50 records or less, you can take the ASCII value of the first character in the string, and that's probably ok. If you need more, take the ASCII values of the first two characters, etc.

If it's a standard "typeable" string, you might want to take the ASCII value and subtract 32, which means each "typical" character would be in the range of 0-94. If that was ok, and you wanted a bigger hash, you could take the ASCII value of the 1st character minus 32, and add it to, say, 95 times the ASCII value of the 2nd character minus 32. And so on.

If I remember my C correctly, a string is simply an array of characters, and you can simply treat a single character as though it were a 1-byte integer (IE, it's already an integer, probably 0-255).

DaveE
 

Similar threads

Replies
55
Views
7K
  • · Replies 34 ·
2
Replies
34
Views
4K
Replies
1
Views
8K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
6K
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K