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;
- //node *table[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[])
- {
- 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));
- dog->next=contn;
- contn->next=anot;
- anot->next=dog;
- char *adword = "change";
- char *pword = "urine";
- char *sword = "hippo";
- node *table[26];
- // copy the characters stored at the ith index
- // of the pointers to characters in addword in the
- // ith index of the word field
- for (int i = 0; adword[i] != '\0'; i++)
- {
- contn->word[i] = adword[i];
- }
- for (int i = 0; pword[i] != '\0'; i++)
- {
- dog->word[i] = pword[i];
- }
- for (int i = 0; pword[i] != '\0'; i++)
- {
- anot->word[i] = sword[i];
- }
- // print table[0]
- for (int i = 0; contn->word[i] != '\0'; i++)
- {
- printf("contn->word[%i] prints: %c\n",i,contn->word[i]);
- printf("contn->next->word[%i] prints: %c\n",i,contn->next->word[i]);
- }
- char *alph = "abcdefghijklmnopqrstuvwxyz";
- char *ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- printf("the character at alph[0] = %c\n", alph[0]);
- printf("the hash value of alph[0] = %i\n", hash(&alph[0]));
- char u = 'u';
- int hu = hash(&u);
- printf("the hash value of the letter u is %i\n", hu);
- printf("hash(adword) prints: %i\n", hash(adword));
- printf("anot->next->word prints %s\n",anot->next->word);
- for (int i = 0; anot->next->word[i] != '\0'; i++)
- {
- printf("anot->next->word[%i] prints %c", i, anot->next->word[i]);
- int hval = hash(anot->next->word);
- printf(" hash(anot->next->word) prints: %i\n", hval);
- //printf(" hash(anot->next->word[%i]) prints: %i\n",i, hash(&anot->next->word[i]));
- }
- char *a = "a";
- int hasha = hash(a);
- int hashb = hash("b");
- int hashc = hash("c");
- char *meani = "turtle";
- int hashm = hash(meani);
- printf("the hash value of character a, hash(a) stored in hasha = %i\n", hasha);
- printf("the hash value of character b, hash(b) stored in hashb = %i\n", hashb);
- printf("the hash value of character c, hash(c) stored in hashc = %i\n", hashc);
- printf("the hash value of character d, hash(d) = %i\n", hash("d"));
- printf("the hash value of character e, hash(e) = %i\n", hash("e"));
- printf("the hash value of character f, hash(f) = %i\n", hash("f"));
- printf("the hash value of character g, hash(g) = %i\n", hash("g"));
- printf("the character at alph[0] is %c and it's hashvalue is %i\n\n", alph[0], hash(&alph[0]));
- printf("the hash value of the word turtle, stored in a pointer to a character called meani, is %i\n",hashm);
- printf("the hash value of t is %i\n", hash("t"));
- printf("the hash value of u is %i\n", hash("u"));
- printf("the hash value of r is %i\n", hash("r"));
- printf("the hash value of t is %i\n", hash("t"));
- printf("the hash value of l is %i\n", hash("l"));
- printf("the hash value of e is %i\n", hash("e"));
- printf("the hash of the letters in meani[] is %i, %i, %i, %i, %i, %i\n", hash("t"),hash("u"),hash("r"),hash("t"),hash("l"),hash("e"));
- // why does the hash value of the first t not appear?
- // output
- // the hash of the letters in meani[] is , 19, 20, 17, 19, 11, 4
- printf("the sum of the letters in meani is %i\n\n",hash("t")+hash("u")+hash("r")+hash("t")+hash("l")+hash("e"));
- for (int i = 0; meani[i] != '\0'; i++)
- {
- char *c = &meani[i];
- int hashcar = hash(c);
- printf("meani[%i] = %c\n", i, meani[i]);
- printf("hashcar = %i\n", hashcar);
- }
- for (int i = 0; meani[i] != '\0'; i++)
- {
- printf("the hash value of meani[%i] = %i\n", i, hash(&meani[i]));
- }
- // why can't i say
- //printf("the hash value of character c, hash(c) stored in hashb = %i\n", hash("c"));
- for (int i = 0; alph[i] != '\0'; i++)
- {
- printf("alph[%i] = %c, it's hash value is %i\n", i, alph[i], hash(&alph[i]));
- }
- for (int i = 0; ALPH[i] != '\0'; i++)
- {
- printf("ALPH[%i] = %c, it's hash value is %i\n", i, ALPH[i], hash(&ALPH[i]));
- }
- free(contn);
- free(dog);
- free(anot);
- }
Advertisement
Add Comment
Please, Sign In to add comment