#include #include #include unsigned int hashf(const char *buf, size_t len); int main(int argc, char *argv[]) { unsigned int i, k, c, hash; if (argc < 3) { puts("Too few arguments!"); return 1; } // Take the filename given as a command line parameter char * filename = argv[1]; // Take the hash table size given as the second parameter int hash_size = atoi(argv[2]); char * table[hash_size]; char * empty = " "; for (int m = 0; m < hash_size; m++) { table[m] = empty; } // char table[hash_size][100]; char word[100]; // Open the file FILE * file = fopen(filename, "r"); if (!file) { puts("Couldn't open the file!"); return 1; } else { int in_word = 0; while ((c = fgetc(file)) != EOF) { word[k] = c; if (c == ' ' || c == '\n' || c == '\t') { if (in_word == 1) { word[k] = '\0'; char* word2 = (char*) malloc((k+1)*sizeof(char)); strcpy(word2, word); hash = hashf(word2, k+1); printf ("word2: %s\n", word2); printf ("length: %d\n", k+1); i = 0; /*while (table[hash] != NULL) { hash += i*i; if (hash > hash_size) { i++; } else { printf("Error: table full"); } }*/ printf ("%u\n", hash); //table[hash] = word2; } k = 0; in_word = 0; } else if (!in_word) { word[k] = c; k++; in_word = 1; } } // Close the file fclose(file); // Print the hash table for (int j = 0; j < hash_size; j++) { printf("%s, ", table[j]); } } return 0; } // Given function that hashes the words unsigned int hashf(const char *buf, size_t len) { unsigned int h = 0; size_t i; for (i = 0; i < len; i++) { h += buf[i]; h += h << 10; h ^= h >> 7; } h += h << 3; h ^= h >> 11; h += h << 15; return h; }