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] = NULL;
- node *table[N];
- // table is not initialized
- // 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++)
- {
- node *modnod = malloc(sizeof(node));
- printf("table[0]->word[%i] = %c\n",i,table[0]->word[i]);
- }
- free(contn);
- free(dog);
- free(anot);
- }
Advertisement
Add Comment
Please, Sign In to add comment