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 table
void initializeHashTable() {
for (int counter = 0; counter < STORAGESIZE; counter++) {
strcpy(hashTable[counter], “”);
}
}
// from Noel Kalicharan
// Advanced topics in C
int convertWordToNumber(char wordToInsert[]) {
/*
intj, wordNum = 0;
intweight = 3;
while (word[j] != ‘\0’) {
wordNum += weight * word[j++];
weight += 2;
}
location = wordNum % n + 1;
return location;
*/
int j = 0, wordNum = 0;
int weight = 3;
while (wordToInsert[j] != '\0') {
wordNum += weight * wordToInsert[j++];
weight += 2;
}
return wordNum;
}
//find next position after a collission
int findSearchedWordPosition(int hashSearchPosition, char l_wordToSearch[]) {
int pos;
for (pos = hashSearchPosition + 1; pos < STORAGESIZE; pos++) {
//if (hashTable[pos] == numberToFind) {
if (hashTable[pos] && strcmp(hashTable[pos], l_wordToSearch) == 0) {
printf("%s %s %d", l_wordToSearch, " Found at ", pos);
return pos;
}
}
if (pos >= STORAGESIZE) {
for (int pos = 0; pos < hashSearchPosition; pos++) {
if (hashTable[pos] && strcmp(hashTable[pos], l_wordToSearch) == 0) {
printf("%s %s %d", l_wordToSearch, " Found at ", pos);
return pos;
}
}
}
return -1;
}
//find a number and return found/not-found
int searchHashTable(char l_wordToSearch[]) {
int numberForTheWord = convertWordToNumber(l_wordToSearch);
int hashSearchPosition = numberForTheWord % N + 1;
if ( hashTable[hashSearchPosition] && (strcmp(hashTable[hashSearchPosition], l_wordToSearch) == 0) ) {
printf("%s %s %d", l_wordToSearch, " Found at ", hashSearchPosition);
return 1;
}
else {
int wordFoundPosition = findSearchedWordPosition(hashSearchPosition, l_wordToSearch);
if (wordFoundPosition > 0) {
printf("%s %s %d", l_wordToSearch, " Found at ", wordFoundPosition);
return 1;
}
}
printf("Word Not Found");
return -1;
}
//find next position after a collission
int findNextAvailablePosition(int index) {
int pos;
for (pos = index + 1; pos < STORAGESIZE; pos++ ) {
//if (hashTable[pos] == 0) {
if (strcmp(hashTable[pos], "") == 0){
return pos;
}
}
if (pos >= STORAGESIZE) {
for (int pos = 0; pos < index; pos++) {
//if (hashTable[pos] == 0) {
if (strcmp(hashTable[pos], "") == 0) {
return pos;
}
}
}
return -1;
}
//insert data into hash table
int insertToHashTable(int l_position_to_insert, char wordToInsert[]) {
//find position to insert
//remainder
int hashPosition = l_position_to_insert % N + 1;
char word[MaxWordLen];
//strcpy(word, hashTable[hashPosition]);
if ( strcmp(hashTable[hashPosition], "") != 0) {
//collision
//so find the next position
hashPosition = findNextAvailablePosition(hashPosition);
}
//error finding a position
if ((hashPosition > STORAGESIZE) || ((hashPosition == -1))) {
// indicates error
printf("\n%s %s\n", "Did not find a Position for ", wordToInsert);
return -1;
}
strcpy(hashTable[hashPosition], wordToInsert);
// 1 indicates success
return 1;
}
//flow of the work
int main() {
//initialize
initializeHashTable();
//read data from file
FILE *in = fopen("input.txt", "r");
char wordToInsert[MaxWordLen];
//insertion
//insert data into the hash table
while (fscanf(in, "%s", &wordToInsert) == 1) {
int positionToInsert = convertWordToNumber(wordToInsert);
insertToHashTable(positionToInsert, wordToInsert);
}
//take input from user to search
//int numberToSearch;
char wordToSearch[MaxWordLen];
printf("\nPlease type the word to search\n");
scanf("%s", wordToSearch);
//find the number in the hash
searchHashTable(wordToSearch);
}