Advertisement
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";
- char *breed = "poodle";
- 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("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->word prints: %s\n",dog->next->word);
- //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("anot->word prints: %s\n",anot->word);
- anot->next = dog;
- printf("word[] = ");
- for (int i = 0; word[i] != '\0'; i++)
- {
- printf("word[%i] = %c",i,word[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
- free(contn);
- free(dog);
- free(anot);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement