joseleeph

Untitled

Oct 9th, 2020
1,485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. //#define LENGTH 26;
  6. typedef struct node
  7. {
  8. char word[26];
  9. struct node *next;
  10. }
  11. node;
  12. const unsigned int N = 26;
  13.  
  14. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  15. {
  16. int val = 0;
  17. //int cval;
  18. for (int i = 0; word[i] != '\0'; i++)
  19. {
  20. //val += word[i];
  21.  
  22. if (isalpha(word[i]))
  23. {
  24. if (isupper(word[i]))
  25. {
  26. val += word[i] - 65;
  27. }
  28. if (islower(word[i]))
  29. {
  30. val += word[i] - 97;
  31. }
  32. if (word[i] == 13)
  33. {
  34. continue;
  35. }
  36. }
  37. else
  38. continue;
  39.  
  40. }
  41. return val%26;
  42. }
  43. int main(int argc, char *argv[])
  44. {
  45. char *word = "caramel";
  46. char *name = "tommy";
  47. char *hi = "hello";
  48. char *greet = "stinkybutt";
  49. char *fish = "shrimp";
  50. char *a = "a";
  51. char *t = "t";
  52. char *s = "sir";
  53. char *test = "codex";
  54. char *alph = "abcdefghijklmnopqrstuvwxyz";
  55. char *ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  56. int val = 0;
  57. int namval = hash(name);
  58. int wval = hash(word);
  59. int hasha = hash(a);
  60. int hasht = hash("t");
  61. int hasho = hash("o");
  62. int hashm = hash("m");
  63. int hashy = hash("y");
  64.  
  65. printf("the hash value of name is %i\n",namval);
  66. printf("the hash value of word is %i\n",wval);
  67. printf("the hash value of a is %i\n",hasha);
  68. printf("the hash value of t is %i\n",hasht);
  69. printf("the hash value of o is %i\n",hasho);
  70. printf("the hash value of m is %i\n",hashm);
  71. printf("the hash value of m is %i\n",hashm);
  72. printf("the hash value of Y is %i\n",hashy);
  73. printf("\n\n");
  74. printf("%i + %i + %i + %i + %i + = %i\n",hasht,hasho,hashm,hashm,hashy,hasht + hasho + hashm + hashm + hashy);
  75.  
  76. printf("these methods don't work\n\n");
  77. printf("the hash value of t is %i\n",hash(&name[0]));
  78. printf("the hash value of o is %i\n",hash(&name[1]));
  79.  
  80. node *poodle = malloc(sizeof(node)); // allocate a space of memory big enough for a node and sotre it in a pointer to a node called poodle
  81. node *contn = malloc(sizeof(node));
  82. node *anot = malloc(sizeof(node));
  83.  
  84. /*
  85. for (int i = 0; test[i] != '\0'; i++)
  86. {
  87. poodle->word[i] = test[i]; // poodle's word field will store codex... the string in test
  88. }
  89. poodle->next = contn->word;// poodle's next field is set to the word field of contn
  90.  
  91. for (int i = 0; greet[i] != '\0'; i++) // greet is stinkybutt
  92. {
  93. contn->word[i] = greet[i]; // contn should print stinkybutt
  94. }
  95. contn->next = anot->word;
  96.  
  97. for (int i = 0; fish[i] != '\0'; i++)
  98. {
  99. anot->word[i] = fish[i];// anot's word field should print shrimp
  100. }
  101. anot->next = poodle->word;
  102. */
  103. printf("word[] = ");
  104. for (int i = 0; word[i] != '\0'; i++)
  105. {
  106. printf("word[%i] = %c",i,word[i]);
  107. printf("\n");
  108. }
  109.  
  110. printf("poodle prints: ");
  111. for (int i = 0; poodle->word[i] != '\0'; i++)
  112. {
  113. printf("%c",poodle->word[i]);
  114. }
  115. printf("\n");
  116.  
  117. printf("anot prints: ");
  118. for (int i = 0; anot->word[i] != '\0'; i++)
  119. {
  120. printf("%c",anot->word[i]);
  121. }
  122. printf("\n");
  123.  
  124.  
  125. /*
  126.  
  127. printf("poodle->next->word prints:\n");
  128. for (int i = 0; poodle->next->word[i] != '\0'; i++)
  129. {
  130. printf("%c",poodle->word->next->word[i]);
  131. }
  132. */
  133.  
  134. node *table[N];// an array of nodes with N elements
  135.  
  136.  
  137. //contn->next = anot->word;
  138. //anot->next = poodle->word;
  139. //printf("poodle->word prints %s"poodle->word);
  140.  
  141. /*
  142. for (int i = 0; poodle[i] != NULL; i++)
  143. {
  144. printf("poodle's next field prints");
  145. printf("%s",poodle[i]->next);
  146. }
  147.  
  148. */
  149. //printf("poodle->next prints %s"poodle->next);
  150.  
  151. //contn->word = "sir";
  152. //test->next = contn->word;
  153.  
  154. /*
  155. printf("the word stored in the test node's word field is %s\n",test->word);
  156. printf("the word stored in the contn node's word field is %s\n",contn->word);
  157. printf("the word stored in the anot node's word field is %s\n",anot->word);
  158.  
  159. printf("the word stored in the test node's next field is %s\n",test->word);
  160. printf("the word stored in the contn node's next field is %s\n",contn->word);
  161. printf("the word stored in the anot node's next field is %s\n",anot->word);
  162.  
  163.  
  164.  
  165. for (int i = 0; i < 26; i++)
  166. {
  167. if (i%2 == 0)
  168. {
  169. table[i]->word = word;
  170. }
  171. if (i%2 != 0)
  172. {
  173. table[i]->word = name;
  174. }
  175. }
  176. */
  177.  
  178.  
  179. printf("the chacter at name[0] is: %c\n",name[0]);//this is correct
  180. printf("the word at name prints: %s\n",name);// this is correct
  181. printf("the hash value of the character t is: %i\n",hasht);// this is correct
  182. printf("the hash value of the character t is: %i\n",hash(t));// this is correct
  183. //printf("hash(name[0] prints %i)",hash(name[0])); ... this method doesn't work
  184. //printf("the hashvalue of name[0] is %i\n",hash(&name[0]));
  185. // this is not correct... prints 13
  186. //printf("the hashvalue of name[1] is %i\n",hash(&name[1]));
  187. // o should hash to 13... incorrect
  188.  
  189. char nz = name[0];
  190. printf("the character at name[0] is %c\n",nz);
  191. printf("the hashvalue of name[0] is %i\n",hash(&nz));// this is correct
  192.  
  193. printf("the hash value of name... stored in the integer namval is: %i\n",namval);
  194. printf("the hash value of the character a is: %i\n",hasha);// this is correct
  195. printf("the hash value of the character a is: %i\n",hash(a));//this is correct
  196. printf("the hash value of the character b is: %i\n",hash("b"));//why double quotes needed?
  197. printf("the hash value of the character t is: %i\n",hash("t"));// t is hashing correctly here
  198.  
  199. printf("word[1] prints %c\n",word[1]);
  200.  
  201. char wo = word[1];
  202. 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
  203. printf("the hash value of word[1] is: %i\n",hash(&wo));
  204. printf("the hash value of wval is: %i\n\n",wval);
  205.  
  206.  
  207. //printf("the hashvalue of alph[0] is %i\n",hash(&alph[0]));
  208. // this does not work
  209.  
  210. // what is this doing?... why does it return 13
  211.  
  212. //printf("the hashvalue of alph[0] is %i\n",hash(&alp));// dereferencing the pointer?
  213.  
  214. printf("the hashvalue of a is %i\n",hasha);// this is correct
  215. printf("the hashvalue of a, or hash(a) is %i\n",hash(a));// these are correct
  216. printf("the hashvalue of t is %i\n",hash(t));
  217. for (int i = 0; alph[i] != '\0'; i++)
  218. {
  219. char c = alph[i];
  220. int alphash = hash(&c);
  221. //int alphash = hash(c);
  222.  
  223. //why&?
  224. printf("alph[%i] = %c\n it's hash value is %i\n",i,alph[i],alphash);// this is weird
  225. }
  226.  
  227. for (int i = 0; ALPH[i] != '\0'; i++)
  228. {
  229. char c = ALPH[i];
  230. //int ALPHASH = hash(&c);
  231.  
  232. //printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],ALPHASH);
  233. printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],hash(&c));
  234.  
  235. }
  236. free(contn);
  237. free(test);
  238. free(anot);
  239. }
Advertisement
Add Comment
Please, Sign In to add comment