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;
- }
- return val%26;
- }
- int main(int argc, char *argv[])
- {
- char *word = "caramel";
- char *name = "tommy";
- char *hi = "hello";
- char *greet = "stinkybutt";
- char *fish = "shrimp";
- char *a = "a";
- char *t = "t";
- char *s = "sir";
- char *test = "codex";
- 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");
- int hasho = hash("o");
- int hashm = hash("m");
- int hashy = hash("y");
- printf("the hash value of name is %i\n",namval);
- printf("the hash value of word is %i\n",wval);
- printf("the hash value of a is %i\n",hasha);
- printf("the hash value of t is %i\n",hasht);
- printf("the hash value of o is %i\n",hasho);
- printf("the hash value of m is %i\n",hashm);
- printf("the hash value of m is %i\n",hashm);
- printf("the hash value of Y is %i\n",hashy);
- printf("\n\n");
- printf("%i + %i + %i + %i + %i + = %i\n",hasht,hasho,hashm,hashm,hashy,hasht + hasho + hashm + hashm + hashy);
- printf("these methods don't work\n\n");
- printf("the hash value of t is %i\n",hash(&name[0]));
- printf("the hash value of o is %i\n",hash(&name[1]));
- node *poodle = malloc(sizeof(node)); // allocate a space of memory big enough for a node and sotre it in a pointer to a node called poodle
- node *contn = malloc(sizeof(node));
- node *anot = malloc(sizeof(node));
- /*
- for (int i = 0; test[i] != '\0'; i++)
- {
- poodle->word[i] = test[i]; // poodle's word field will store codex... the string in test
- }
- poodle->next = contn->word;// poodle's next field is set to the word field of contn
- for (int i = 0; greet[i] != '\0'; i++) // greet is stinkybutt
- {
- contn->word[i] = greet[i]; // contn should print stinkybutt
- }
- contn->next = anot->word;
- for (int i = 0; fish[i] != '\0'; i++)
- {
- anot->word[i] = fish[i];// anot's word field should print shrimp
- }
- anot->next = poodle->word;
- */
- printf("word[] = ");
- for (int i = 0; word[i] != '\0'; i++)
- {
- printf("word[%i] = %c",i,word[i]);
- printf("\n");
- }
- printf("poodle prints: ");
- for (int i = 0; poodle->word[i] != '\0'; i++)
- {
- printf("%c",poodle->word[i]);
- }
- printf("\n");
- printf("anot prints: ");
- for (int i = 0; anot->word[i] != '\0'; i++)
- {
- printf("%c",anot->word[i]);
- }
- printf("\n");
- /*
- printf("poodle->next->word prints:\n");
- for (int i = 0; poodle->next->word[i] != '\0'; i++)
- {
- printf("%c",poodle->word->next->word[i]);
- }
- */
- node *table[N];// an array of nodes with N elements
- //contn->next = anot->word;
- //anot->next = poodle->word;
- //printf("poodle->word prints %s"poodle->word);
- /*
- for (int i = 0; poodle[i] != NULL; i++)
- {
- printf("poodle's next field prints");
- printf("%s",poodle[i]->next);
- }
- */
- //printf("poodle->next prints %s"poodle->next);
- //contn->word = "sir";
- //test->next = contn->word;
- /*
- printf("the word stored in the test node's word field is %s\n",test->word);
- printf("the word stored in the contn node's word field is %s\n",contn->word);
- printf("the word stored in the anot node's word field is %s\n",anot->word);
- printf("the word stored in the test node's next field is %s\n",test->word);
- printf("the word stored in the contn node's next field is %s\n",contn->word);
- printf("the word stored in the anot node's next field is %s\n",anot->word);
- for (int i = 0; i < 26; i++)
- {
- if (i%2 == 0)
- {
- table[i]->word = word;
- }
- if (i%2 != 0)
- {
- table[i]->word = name;
- }
- }
- */
- 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
- char nz = name[0];
- printf("the character at name[0] is %c\n",nz);
- printf("the hashvalue of name[0] is %i\n",hash(&nz));// this is correct
- 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("word[1] prints %c\n",word[1]);
- char wo = word[1];
- 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 word[1] is: %i\n",hash(&wo));
- printf("the hash value of wval is: %i\n\n",wval);
- //printf("the hashvalue of alph[0] is %i\n",hash(&alph[0]));
- // this does not work
- // what is this doing?... why does it return 13
- //printf("the hashvalue of alph[0] is %i\n",hash(&alp));// dereferencing the pointer?
- 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];
- //int ALPHASH = hash(&c);
- //printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],ALPHASH);
- printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],hash(&c));
- }
- free(contn);
- free(test);
- free(anot);
- }
Advertisement
Add Comment
Please, Sign In to add comment