Hashing a string

This is a discussion on Hashing a string within the C Programming forums, part of the General Programming Boards category; I need to write a hash function where I take the ascii values of every letter in a string and ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    46

    Hashing a string

    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    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
    Why not do
    sum = key[0] + key[1] + key[2]
    and cut out the pointless sprintf / sscanf steps.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    Well it's part of the assignment:
    Name:  00000asciihash.PNG
Views: 223
Size:  8.0 KB

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your example in post#3 translates 'a' as 61 (the ascii value in hex), then 'n' as 110 (ascii in decimal), then 'a' as 64 (hex again). Is that what you're supposed to do?

    What are you supposed to do if you have more than 9 digits in values?

    If you're only supposed to deal with the first 9 chars then it hardly seems necessary to realloc values. You could simply declare a local char array of, say, 15 chars and end the loop when it has at least 9 chars in it.

    It's not obvious why your program would crash when you free values, but that kind of thing can happen if you've messed up the heap somehow somewhere.

    Why is pKey void*? Why not char*?

    "temp" is a bad variable name if you can think of something better. How about "ascii"?

    Get rid of the sprintf into values (and its controlling if); just use the strcat (values is initially all zeros so strcat will copy the first string to the beginning anyway).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by oogabooga View Post
    What are you supposed to do if you have more than 9 digits in values?
    Also, what do you do if you only have six digits?

    "cab" = 676566 = 676566???


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String hashing
    By rags123 in forum C Programming
    Replies: 1
    Last Post: 04-03-2010, 08:47 AM
  2. String hashing
    By jack_carver in forum C Programming
    Replies: 19
    Last Post: 09-11-2009, 08:32 PM
  3. string hashing algorithm
    By cyberfish in forum Tech Board
    Replies: 4
    Last Post: 04-11-2009, 01:25 PM
  4. hashing a string to an int question
    By rakan in forum C++ Programming
    Replies: 9
    Last Post: 11-16-2006, 11:35 AM
  5. hashing a string
    By cozman in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 12:42 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21