joseleeph

Untitled

Oct 21st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. //#define LENGTH 26;
  6.  
  7. typedef struct node
  8. {
  9. char word[26];
  10. struct node *next;
  11. }
  12. node;
  13. const unsigned int N = 26;
  14. //node *table[26];
  15.  
  16.  
  17. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  18. {
  19. int val = 0;
  20. //int cval;
  21. for (int i = 0; word[i] != '\0'; i++)
  22. {
  23. //val += word[i];
  24.  
  25. if (isalpha(word[i]))
  26. {
  27. if (isupper(word[i]))
  28. {
  29. val += word[i] - 65;
  30. }
  31. if (islower(word[i]))
  32. {
  33. val += word[i] - 97;
  34. }
  35. if (word[i] == 13)
  36. {
  37. continue;
  38. }
  39. }
  40. else
  41. continue;
  42.  
  43. }
  44. return val%26;
  45. }
  46. int main(int argc, char *argv[])
  47. {
  48.  
  49.  
  50. 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
  51. node *contn = malloc(sizeof(node));
  52. node *anot = malloc(sizeof(node));
  53.  
  54. dog->next=contn;
  55. contn->next=anot;
  56. anot->next=dog;
  57.  
  58. char *adword = "change";
  59. char *pword = "urine";
  60. char *sword = "hippo";
  61.  
  62.  
  63.  
  64. node *table[26];
  65.  
  66.  
  67. // copy the characters stored at the ith index
  68. // of the pointers to characters in addword in the
  69. // ith index of the word field
  70. for (int i = 0; adword[i] != '\0'; i++)
  71. {
  72. contn->word[i] = adword[i];
  73. }
  74.  
  75. for (int i = 0; pword[i] != '\0'; i++)
  76. {
  77. dog->word[i] = pword[i];
  78. }
  79. for (int i = 0; pword[i] != '\0'; i++)
  80. {
  81. anot->word[i] = sword[i];
  82. }
  83.  
  84.  
  85. // print table[0]
  86. for (int i = 0; contn->word[i] != '\0'; i++)
  87. {
  88. printf("contn->word[%i] prints: %c\n",i,contn->word[i]);
  89. printf("contn->next->word[%i] prints: %c\n",i,contn->next->word[i]);
  90. }
  91.  
  92. char *alph = "abcdefghijklmnopqrstuvwxyz";
  93. char *ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  94.  
  95. printf("the character at alph[0] = %c\n", alph[0]);
  96. printf("the hash value of alph[0] = %i\n", hash(&alph[0]));
  97.  
  98.  
  99. char u = 'u';
  100. int hu = hash(&u);
  101. printf("the hash value of the letter u is %i\n", hu);
  102.  
  103.  
  104.  
  105. printf("hash(adword) prints: %i\n", hash(adword));
  106. printf("anot->next->word prints %s\n",anot->next->word);
  107. for (int i = 0; anot->next->word[i] != '\0'; i++)
  108. {
  109. printf("anot->next->word[%i] prints %c", i, anot->next->word[i]);
  110. int hval = hash(anot->next->word);
  111. printf(" hash(anot->next->word) prints: %i\n", hval);
  112. //printf(" hash(anot->next->word[%i]) prints: %i\n",i, hash(&anot->next->word[i]));
  113. }
  114.  
  115. char *a = "a";
  116. int hasha = hash(a);
  117. int hashb = hash("b");
  118. int hashc = hash("c");
  119. char *meani = "turtle";
  120. int hashm = hash(meani);
  121. printf("the hash value of character a, hash(a) stored in hasha = %i\n", hasha);
  122. printf("the hash value of character b, hash(b) stored in hashb = %i\n", hashb);
  123. printf("the hash value of character c, hash(c) stored in hashc = %i\n", hashc);
  124. printf("the hash value of character d, hash(d) = %i\n", hash("d"));
  125. printf("the hash value of character e, hash(e) = %i\n", hash("e"));
  126. printf("the hash value of character f, hash(f) = %i\n", hash("f"));
  127. printf("the hash value of character g, hash(g) = %i\n", hash("g"));
  128.  
  129.  
  130.  
  131.  
  132. printf("the character at alph[0] is %c and it's hashvalue is %i\n\n", alph[0], hash(&alph[0]));
  133. printf("the hash value of the word turtle, stored in a pointer to a character called meani, is %i\n",hashm);
  134. printf("the hash value of t is %i\n", hash("t"));
  135. printf("the hash value of u is %i\n", hash("u"));
  136. printf("the hash value of r is %i\n", hash("r"));
  137. printf("the hash value of t is %i\n", hash("t"));
  138. printf("the hash value of l is %i\n", hash("l"));
  139. printf("the hash value of e is %i\n", hash("e"));
  140. 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"));
  141. // why does the hash value of the first t not appear?
  142. // output
  143. // the hash of the letters in meani[] is , 19, 20, 17, 19, 11, 4
  144. printf("the sum of the letters in meani is %i\n\n",hash("t")+hash("u")+hash("r")+hash("t")+hash("l")+hash("e"));
  145.  
  146.  
  147. for (int i = 0; meani[i] != '\0'; i++)
  148. {
  149. char *c = &meani[i];
  150. int hashcar = hash(c);
  151. printf("meani[%i] = %c\n", i, meani[i]);
  152. printf("hashcar = %i\n", hashcar);
  153. }
  154. for (int i = 0; meani[i] != '\0'; i++)
  155. {
  156. printf("the hash value of meani[%i] = %i\n", i, hash(&meani[i]));
  157. }
  158. // why can't i say
  159. //printf("the hash value of character c, hash(c) stored in hashb = %i\n", hash("c"));
  160.  
  161. for (int i = 0; alph[i] != '\0'; i++)
  162. {
  163. printf("alph[%i] = %c, it's hash value is %i\n", i, alph[i], hash(&alph[i]));
  164. }
  165.  
  166.  
  167. for (int i = 0; ALPH[i] != '\0'; i++)
  168. {
  169. printf("ALPH[%i] = %c, it's hash value is %i\n", i, ALPH[i], hash(&ALPH[i]));
  170. }
  171.  
  172.  
  173. free(contn);
  174. free(dog);
  175. free(anot);
  176. }
  177.  
Add Comment
Please, Sign In to add comment