{"id":76169,"date":"2024-07-20T19:47:43","date_gmt":"2024-07-20T23:47:43","guid":{"rendered":"https:\/\/bangla.sitestree.com\/?p=76169"},"modified":"2024-07-28T17:02:54","modified_gmt":"2024-07-28T21:02:54","slug":"build-a-word-hash","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=76169","title":{"rendered":"Build a Word Hash"},"content":{"rendered":"\n<p>Store Words in a Hash Table. Also, search a word in that Hash Table<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">define _CRT_SECURE_NO_WARNINGS<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<p>\/\/#define MaxNumbers 100<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">define MaxWordLen 50<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">define Empty 0<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">define STORAGESIZE 15<\/h1>\n\n\n\n<p>\/\/ to divide with<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">define N 15<\/h1>\n\n\n\n<p>char hashTable[STORAGESIZE][MaxWordLen];<\/p>\n\n\n\n<p>\/\/initialize hash table<br \/>void initializeHashTable() {<br \/>for (int counter = 0; counter &lt; STORAGESIZE; counter++) {<br \/>strcpy(hashTable[counter], &#8220;&#8221;);<br \/>}<br \/>}<\/p>\n\n\n\n<p>\/\/ from Noel Kalicharan<br \/>\/\/ Advanced topics in C<br \/>int convertWordToNumber(char wordToInsert[]) {<br \/>\/*<br \/>intj, wordNum = 0;<br \/>intweight = 3;<br \/>while (word[j] != &#8216;\\0&#8217;) {<br \/>wordNum += weight * word[j++];<br \/>weight += 2;<br \/>}<br \/>location = wordNum % n + 1;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return location;\n\n*\/\n\nint j = 0, wordNum = 0;\nint weight = 3; \n\nwhile (wordToInsert&#91;j] != '\\0') {\n    wordNum += weight * wordToInsert&#91;j++];\n    weight += 2;\n}\n\nreturn wordNum;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/find next position after a collission<br \/>int findSearchedWordPosition(int hashSearchPosition, char l_wordToSearch[]) {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int pos;\nfor (pos = hashSearchPosition + 1; pos &lt; STORAGESIZE; pos++) {\n    \/\/if (hashTable&#91;pos] == numberToFind) {\n    if (hashTable&#91;pos] &amp;&amp; strcmp(hashTable&#91;pos], l_wordToSearch) == 0) {\n        printf(\"%s %s %d\", l_wordToSearch, \" Found at \", pos);\n        return pos;\n    }\n}\n\nif (pos &gt;= STORAGESIZE) {\n    for (int pos = 0; pos &lt; hashSearchPosition; pos++) {\n        if (hashTable&#91;pos] &amp;&amp; strcmp(hashTable&#91;pos], l_wordToSearch) == 0) {\n            printf(\"%s %s %d\", l_wordToSearch, \" Found at \", pos);\n            return pos;\n\n        }\n    }\n}\n\n\nreturn -1;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/find a number and return found\/not-found<br \/>int searchHashTable(char l_wordToSearch[]) {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int numberForTheWord = convertWordToNumber(l_wordToSearch);\nint hashSearchPosition = numberForTheWord % N + 1;\n\nif ( hashTable&#91;hashSearchPosition] &amp;&amp; (strcmp(hashTable&#91;hashSearchPosition], l_wordToSearch) == 0)   ) {\n    printf(\"%s %s %d\", l_wordToSearch, \" Found at \", hashSearchPosition);\n    return 1;\n}\nelse {\n\n    int wordFoundPosition = findSearchedWordPosition(hashSearchPosition, l_wordToSearch);\n    if (wordFoundPosition &gt; 0) {\n        printf(\"%s %s %d\", l_wordToSearch, \" Found at \", wordFoundPosition);\n        return 1;\n    }\n\n\n\n}\n\nprintf(\"Word Not Found\");\nreturn -1;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/find next position after a collission<br \/>int findNextAvailablePosition(int index) {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int pos;\nfor (pos = index + 1; pos &lt; STORAGESIZE; pos++ ) {\n    \/\/if (hashTable&#91;pos] == 0) {\n    if (strcmp(hashTable&#91;pos], \"\") == 0){\n        return pos;\n    }\n}\n\nif (pos &gt;= STORAGESIZE) {\n    for (int pos = 0; pos &lt; index; pos++) {\n        \/\/if (hashTable&#91;pos] == 0) {\n        if (strcmp(hashTable&#91;pos], \"\") == 0) {\n            return pos;\n        }\n    }\n}\n\n\nreturn -1;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/insert data into hash table<br \/>int insertToHashTable(int l_position_to_insert, char wordToInsert[]) {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/find position to insert\n\/\/remainder\nint hashPosition = l_position_to_insert % N + 1;\n\nchar word&#91;MaxWordLen];\n\/\/strcpy(word, hashTable&#91;hashPosition]);\n\nif (  strcmp(hashTable&#91;hashPosition], \"\") != 0) {\n    \/\/collision\n    \/\/so find the next position\n    hashPosition = findNextAvailablePosition(hashPosition);\n}\n\n\/\/error finding a position \nif ((hashPosition &gt; STORAGESIZE) || ((hashPosition == -1))) {\n    \/\/ indicates error\n    printf(\"\\n%s %s\\n\", \"Did not find a Position for \", wordToInsert);\n    return -1;\n}\n\nstrcpy(hashTable&#91;hashPosition], wordToInsert);\n\n\/\/ 1 indicates success\nreturn 1;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/flow of the work<br \/>int main() {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/initialize\ninitializeHashTable();\n\n\/\/read data from file\nFILE *in = fopen(\"input.txt\", \"r\");\n\nchar wordToInsert&#91;MaxWordLen];\n\n\n\/\/insertion\n\/\/insert data into the hash table\nwhile (fscanf(in, \"%s\", &amp;wordToInsert) == 1) {\n    int positionToInsert = convertWordToNumber(wordToInsert);\n    insertToHashTable(positionToInsert, wordToInsert);\n}\n\n\/\/take input from user to search\n\/\/int numberToSearch;\nchar wordToSearch&#91;MaxWordLen];\nprintf(\"\\nPlease type the word to search\\n\");\nscanf(\"%s\", wordToSearch);\n\n\/\/find the number in the hash\nsearchHashTable(wordToSearch);<\/code><\/pre>\n\n\n\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Store Words in a Hash Table. Also, search a word in that Hash Table define _CRT_SECURE_NO_WARNINGS include include include \/\/#define MaxNumbers 100 define MaxWordLen 50 define Empty 0 define STORAGESIZE 15 \/\/ to divide with define N 15 char hashTable[STORAGESIZE][MaxWordLen]; \/\/initialize hash tablevoid initializeHashTable() {for (int counter = 0; counter &lt; STORAGESIZE; counter++) {strcpy(hashTable[counter], &#8220;&#8221;);}} &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=76169\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1966,1883,182],"tags":[],"class_list":["post-76169","post","type-post","status-publish","format-standard","hentry","category-data-structure-and-algorithms","category---data-structure","category---blog","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":76171,"url":"http:\/\/bangla.sitestree.com\/?p=76171","url_meta":{"origin":76169,"position":0},"title":"Build a Number Hash in C","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"Create a Hash Table to store numbers. Also, search numbers in that Hash Table. Write Code in C define _CRT_SECURE_NO_WARNINGS include include include define MaxNumbers 50 \/\/ to divide with define N 100 define Empty 0 define STORAGESIZE 100 int hashTable[STORAGESIZE + 1]; \/\/initialize hash tablevoid initializeHashTable() { for (int\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76197,"url":"http:\/\/bangla.sitestree.com\/?p=76197","url_meta":{"origin":76169,"position":1},"title":"Steps to Build a Hash Table: Using Graph: Chaining","author":"Sayed","date":"July 28, 2024","format":false,"excerpt":"\u2022Build the Hash \u2022Read a number\/word from file \u2022Find the position for this number\/word \u2022in the graph (vertical array of nodes) \u2022By dividing with N or similar \u2022Check if any vertex node is there \u2022In that array position (array of graph vertices) \u2022If no vertex is there \u2022Create a vertex\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76199,"url":"http:\/\/bangla.sitestree.com\/?p=76199","url_meta":{"origin":76169,"position":2},"title":"Steps to Search in a Hash Table Built Using Graph: Chaining","author":"Sayed","date":"July 28, 2024","format":false,"excerpt":"\u2022Search the Hash \u2022Read a number\/word \u2022From the user \u2022Find the position for this number\/word \u2022in the graph (vertical array of nodes) \u2022By dividing with N or similar \u2022Check if any vertex node is there \u2022In that array position (array of graph vertices) \u2022If no vertex is there \u2022The word\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76183,"url":"http:\/\/bangla.sitestree.com\/?p=76183","url_meta":{"origin":76169,"position":3},"title":"Struct and Tree Node Examples","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"#include <iostream> #include <string.h> #pragma warning(disable : 4996) #define MaxWordSize 100 \/\/ Declare a structure that holds data in a node typedef struct { int num; } NodeDataInt; \/\/ Declare a structure that holds data in a node typedef struct { char word[MaxWordSize + 1]; int freq; } NodeDataChar; \/\/\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69017,"url":"http:\/\/bangla.sitestree.com\/?p=69017","url_meta":{"origin":76169,"position":4},"title":"Logical Data Modeling: Logical Database Design Steps: RDBMS #6","author":"Author-Check- Article-or-Video","date":"August 10, 2021","format":false,"excerpt":"Logical Data Modeling Identify major entities Determine relationships between entities Determine primary and alternate keys Determine foreign keys Determine key business rules Add remaining attributes Validate user views through normalization Determine domains Determine triggering operations Combine user views Integrate with existing data models Analyze for stability and growth Translate Logical\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76181,"url":"http:\/\/bangla.sitestree.com\/?p=76181","url_meta":{"origin":76169,"position":5},"title":"Struct and Tree Node","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"\/\/ Ref: typedef and struct \/\/ https:\/\/www.w3resource.com\/c-programming-exercises\/c-snippets\/difference-between-typedef-struct-and-struct-definitions-with-example.php#google_vignette \/\/ https:\/\/www.tutorialspoint.com\/cprogramming\/c_pointers.htm \/\/ https:\/\/www.geeksforgeeks.org\/typedef-in-c\/ #pragma warning(disable : 4996) #include <iostream> #include <string.h> \/\/ Declare a structure that holds data in a node typedef struct { int num; } NodeData; \/\/ define what a node will look like typedef struct treenode { NodeData data;\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76169"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76169\/revisions"}],"predecessor-version":[{"id":76170,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76169\/revisions\/76170"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76169"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}