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

In summary, the conversation discusses how to represent a string of characters as an integer for a key variable in building a hash table. It is suggested to use the ASCII values of the characters, with options such as subtracting 32 from the ASCII value for a "typable" string or combining the ASCII values of multiple characters. It is also noted that a string can be treated as a 1-byte integer in C.
  • #1
unknown_2
29
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
  • #2
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
 

1. What is the difference between strings and integers in C?

In C, strings are considered to be a sequence of characters, while integers are numerical values. Strings are enclosed in double quotes, while integers are just numbers without any quotation marks. Additionally, strings can contain any character, including letters and symbols, while integers can only contain numerical values.

2. How do I convert a string to an integer in C?

To convert a string to an integer in C, you can use the atoi() function. This function takes a string as an argument and returns an integer value. However, it is important to note that this function does not perform any error checking, so it is important to ensure that the string being converted only contains numerical values.

3. Can a string be used in place of an integer in C?

No, a string cannot be used in place of an integer in C. Since strings and integers are different data types, they cannot be used interchangeably. Attempting to do so will result in a compilation error.

4. How do I convert an integer to a string in C?

To convert an integer to a string in C, you can use the sprintf() function. This function takes a string as the first argument and an integer as the second argument, and it converts the integer to a string and stores it in the provided string. It is important to ensure that the string has enough space to hold the converted integer.

5. Can I perform arithmetic operations on strings in C?

No, you cannot perform arithmetic operations on strings in C. Since strings are not numerical values, it is not possible to use them in mathematical operations. Attempting to do so will result in a compilation error.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
3
Views
891
Back
Top