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 *animal = "buffalo";
- 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";
- char *breed = "poodle";
- int val = 0;
- int namval = hash(name);
- int aval = hash(animal);
- 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 animal is %i\n",aval);
- 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("81 modulus 26 = %i\n\n",81%26);
- 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\n",hash(&name[1]));
- node *dog = 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; breed[i] != '\0'; i++)// '' not required?
- {
- dog->word[i] = breed[i];
- }
- printf("dog->word prints: %s\n",dog->word);
- dog->next = contn;
- //printf("dog->next prints: %s\n",dog->next);
- for (int i = 0; fish[i] != '\0'; i++)
- {
- contn->word[i] = fish[i];
- }
- printf("contn->word prints: %s\n",contn->word);
- contn->next = anot;
- printf("contn->next->word prints: %s\n",contn->next->word);
- //printf("contn->next prints: %s",contn->next);
- for (int i = 0; greet[i] != '\0'; i++)
- {
- anot->word[i] = greet[i];
- }
- printf("contn->next->word prints: %s\n",contn->next->word);
- printf("anot->word prints: %s\n",anot->word);
- anot->next = dog;
- printf("dog->next->word prints: %s\n",dog->next->word);
- printf("animal[] = \n");
- for (int i = 0; animal[i] != '\0'; i++)
- {
- printf("animal[%i] = %c",i,animal[i]);
- printf("\n");
- }
- printf("dog prints: ");
- for (int i = 0; dog->word[i] != '\0'; i++)
- {
- printf("%c",dog->word[i]);
- }
- printf("\n");
- printf("anot prints: ");
- for (int i = 0; anot->word[i] != '\0'; i++)
- {
- printf("%c",anot->word[i]);
- }
- printf("\n");
- node *table[N];// an array of nodes with N elements
- table[0]->word[0] = 'c';
- table[0]->word[1] = 'h';
- table[0]->word[2] = 'a';
- table[0]->word[3] = 'n';
- table[0]->word[4] = 'g';
- table[0]->word[5] = 'e';
- table[0]->word[6] = '\0';
- table[0]->next = table[1];
- //->word[1];
- table[1]->word[0] = 'f';
- table[1]->word[1] = 'o';
- table[1]->word[2] = 'r';
- table[1]->word[3] = 'g';
- table[1]->word[4] = 'e';
- table[1]->word[5] = '\0';
- table[1]->next = table[2];
- table[2]->word[0] = 's';
- table[2]->word[1] = 'e';
- table[2]->word[2] = 'b';
- table[2]->word[3] = 'u';
- table[2]->word[4] = 'm';
- table[2]->word[5] = '\0';
- table[2]->next = table[0];
- //->word[0];
- for (int i = 0; table[0]->word[i] != '\0'; i++)
- {
- printf("table[0]->word[%i] = %c\n",i,table[0]->word[i]);
- }
- free(contn);
- free(dog);
- free(anot);
- }
- /*
- printf("poodle->next->word prints:\n");
- for (int i = 0; poodle->next->word[i] != '\0'; i++)
- {
- printf("%c",poodle->word->next->word[i]);
- }
- 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);
- 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]));
- 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));
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment