Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- //#define LENGTH 26;
- typedef struct node
- {
- char word[26];
- struct node *next;
- }
- node;
- const unsigned int N = 26;
- unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
- {
- int val = 0;
- //int cval;
- for (int i = 0; word[i] != '\0'; i++)
- {
- //val += word[i];
- if (isalpha(word[i]))
- {
- if (isupper(word[i]))
- {
- val += word[i] - 65;
- }
- if (islower(word[i]))
- {
- val += word[i] - 97;
- }
- if (word[i] == 13)
- {
- continue;
- }
- }
- else
- continue;
- }
- // TODO
- return val%26;
- // maybe try a hash table that will use the first 3 letters
- //return 0;
- }
- int main(int argc, char *argv[])
- {
- char *word = "caramel";
- char *name = "tommy";
- char *a = "a";
- char *t = "t";
- char *alph = "abcdefghijklmnopqrstuvwxyz";
- char *ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int val = 0;
- int namval = hash(name);
- int wval = hash(word);
- int hasha = hash(a);
- int hasht = hash(t);
- node *test;
- node *contn;
- test->word = "hello";
- contn->word = "sir";
- test->next = contn->word;
- node *table[N];
- printf("the word stored in the test node's word field is %s\n",test->word);
- printf("the chacter at name[0] is: %c\n",name[0]);//this is correct
- printf("the word at name prints: %s\n",name);// this is correct
- printf("the hash value of the character t is: %i\n",hasht);// this is correct
- printf("the hash value of the character t is: %i\n",hash(t));// this is correct
- //printf("hash(name[0] prints %i)",hash(name[0])); ... this method doesn't work
- printf("the hashvalue of name[0] is %i\n",hash(&name[0]));// this is not correct... prints 13
- printf("the hashvalue of name[1] is %i\n",hash(&name[1]));// o should hash to 13... incorrect
- printf("the hash value of name... stored in the integer namval is: %i\n",namval);
- printf("the hash value of the character a is: %i\n",hasha);// this is correct
- printf("the hash value of the character a is: %i\n",hash(a));//this is correct
- printf("the hash value of the character b is: %i\n",hash("b"));//why double quotes needed?
- printf("the hash value of the character t is: %i\n",hash("t"));// t is hashing correctly here
- printf("the hash value of word[1] is: %i\n",hash(&word[1]));// what is this doing?... would expect it to return 2 for c
- printf("the hash value of wval is: %i\n\n",wval);
- printf("the character at alph[0] is %c\n",alph[0]);
- printf("the hashvalue of alph[0] is %i\n",hash(&alph[0]));// what is this doing?
- printf("the hashvalue of a is %i\n",hasha);// this is correct
- printf("the hashvalue of a, or hash(a) is %i\n",hash(a));// these are correct
- printf("the hashvalue of t is %i\n",hash(t));
- for (int i = 0; alph[i] != '\0'; i++)
- {
- char c = alph[i];
- int alphash = hash(&c);
- //int alphash = hash(c);
- //why&?
- printf("alph[%i] = %c\n it's hash value is %i\n",i,alph[i],alphash);// this is weird
- }
- for (int i = 0; ALPH[i] != '\0'; i++)
- {
- char c = ALPH[i];
- printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],hash(&c));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment