Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:23:07+00:00 2026-05-28T00:23:07+00:00

I’m working of my assignment, in C, which to store 500 string into strings

  • 0

I’m working of my assignment, in C, which to store 500 string into strings of 5 char by the mean of hash table with chaining method to fix the collision.

Hashing algorithm : to add up the ASCII value and apply the modulus operator to the result.

The hash table store the hash key generated and a pointer which points to a linked list. Each linked list has more than one element if there are more than one 5-char string that gives the same hash key.

So far this is my code. I compiled it (Codeblock) and it appears that there is no error. However the program crashed.

Please give some inputs on where did i do wrong.

#include <stdio.h>
#include <string.h>

#define SLEN 500
#define WLEN 5
#define MPRIME 73

struct Node {
    char s[WLEN+1];      // array to hold the 5-letter word
    int sindex;          // starting index of the word
    struct Node * next;  // a pointer to the next word in the list
};

int searchword(char *);
int hashfunc(char *);
void build_hashtbl();

struct Node * hashtable[MPRIME] = {NULL};

char string[SLEN+1] = "thenamewasfamiliartomeonseverallevelslookingbackitwasfatethatifoundhimihadcometopeppervillebeachtocloseonasmallhousethathadbeeninourfamilyforyearsonmywaybacktotheairportistoppedforcoffeetherewasafieldacrossthestreetwherekidsinpurpletshirtswerepitchingandhittingihadtimeiwanderedoverasistoodatthebackstopmyfingercurledinthechainlinkfenceanoldmanmaneuveredalawnmoweroverthegrasshewastannedandwrinkledwithahalfcigarinhismouthheshutthemowerwhenhesawmeandaskedifihadakidoutthereisaidnoheaskedwhatiwasdoing";

int main(void) {
    int index;
    char query[WLEN+1];
    build_hashtbl();      // prepare the hash table
    printf("Enter a 5-letter word to search: ");
    scanf("%s", query);
    index = searchword(query);
    if (index != -1)
        printf("The word %s starts at index %d.\n", query, index);
    else
        printf("The word %s is not found.\n", query);
    return 0;
}

int searchword(char * word) {
    int hashval;
    struct Node * lhead;
    hashval = hashfunc(word);
    lhead = hashtable[hashval];
    while (lhead) {
        if (strcmp(lhead->s,word) == 0)
            return lhead->sindex;
        lhead = lhead->next;
    }
    return -1;
}

int hashfunc(char *){
    int hashval = 0;
    int i = 0;
    for (i = 0; i < WLEN; i++){
        hashval += (int) string[i];
    }
    return (int) (hashval % MPRIME);
}

void build_hashtbl(){
    struct Node *hashtable[MPRIME]; //already declared. put here for ease
    struct Node * head = NULL;
    struct Node * last = NULL;

    int i = 0;
    int k = 0;
    int key = 0;
    char sElement[WLEN+1] = {0};

    for (i = 0; i <SLEN; i = i+WLEN){ //for every 5 char, find they hashtable index key
        key = hashfunc(*string[i]);

        for (k = 0; k <WLEN; k++){ //create a new string, sElement from the 5 letter word
        sElement[k] = string[i+k];
        }



    if (hashtable[key] != (NULL)){  //if the hashtable element at that index is empty, STORE it in a node
        hashtable[key] = head;
        struct Node *new_node;
        new_node = (struct Node *) malloc ( sizeof (struct Node) );
        strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
        new_node->sindex = i; //put the starting index of this word
        new_node->next = NULL; //the next pointer is set to NULL
        head->next = new_node; //finally set the head node to point to this new node
        last = new_node; //set the new node as the last node
    }
    else { //if there is already a node in the array
        struct Node *new_node;
        new_node = (struct Node *) malloc ( sizeof (struct Node) );
        strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
        new_node->sindex = i; //put the starting index of this word
        new_node->next = NULL; //the next pointer is set to NULL
        head->next = new_node; //finally set the head node to point to this new node
        last->next = new_node; //set the last node to point to thew new created node
        last = new_node; //set the new node as the last node
    }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:23:08+00:00Added an answer on May 28, 2026 at 12:23 am

    Your used last and head uninitialized, so head->next and friends would segfault. In fact you don’t need them at all and you don’t need your if branches – just replace hashtable[key] by new_node after setting new_node->next to hashtable[key]

    void build_hashtbl(){
    
        int i = 0;
        int k = 0;
        int key = 0;
    
        char sElement[WLEN+1] = {0};
    
        for (i = 0; i <SLEN; i = i+WLEN){ //for every 5 char, find they hashtable index key
            key = hashfunc(string+i);
    
            for (k = 0; k <WLEN; k++){ //create a new string, sElement from the 5 letter word
                sElement[k] = string[i+k];
            }
    
            struct Node *new_node;
            new_node = (struct Node *) malloc ( sizeof (struct Node) );
            strcpy(new_node->s, sElement); //put the new 5 letter word string into the node
            new_node->next=hashtable[key];
            new_node->sindex=i;
            hashtable[key]=new_node;
    
        }
    }
    

    Works for me.

    Edit: Also needs #include <stdlib.h> (at least here)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.