joseleeph

Untitled

Oct 19th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 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 *animal = "buffalo";
  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. char *breed = "poodle";
  57. int val = 0;
  58. int namval = hash(name);
  59. int aval = hash(animal);
  60. int hasha = hash(a);
  61. int hasht = hash("t");
  62. int hasho = hash("o");
  63. int hashm = hash("m");
  64. int hashy = hash("y");
  65.  
  66. printf("the hash value of name is %i\n",namval);
  67. printf("the hash value of animal is %i\n",aval);
  68. printf("the hash value of a is %i\n",hasha);
  69. printf("the hash value of t is %i\n",hasht);
  70. printf("the hash value of o is %i\n",hasho);
  71. printf("the hash value of m is %i\n",hashm);
  72. printf("the hash value of m is %i\n",hashm);
  73. printf("the hash value of Y is %i\n",hashy);
  74. printf("\n\n");
  75. printf("%i + %i + %i + %i + %i = %i\n",hasht,hasho,hashm,hashm,hashy,hasht + hasho + hashm + hashm + hashy);
  76. printf("81 modulus 26 = %i\n\n",81%26);
  77.  
  78. printf("these methods don't work\n\n");
  79. printf("the hash value of t is %i\n",hash(&name[0]));
  80. printf("the hash value of o is %i\n\n",hash(&name[1]));
  81.  
  82. 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
  83. node *contn = malloc(sizeof(node));
  84. node *anot = malloc(sizeof(node));
  85.  
  86. for (int i = 0; breed[i] != '\0'; i++)// '' not required?
  87. {
  88. dog->word[i] = breed[i];
  89. }
  90. printf("dog->word prints: %s\n",dog->word);
  91. dog->next = contn;
  92.  
  93. //printf("dog->next prints: %s\n",dog->next);
  94.  
  95. for (int i = 0; fish[i] != '\0'; i++)
  96. {
  97. contn->word[i] = fish[i];
  98. }
  99. printf("contn->word prints: %s\n",contn->word);
  100. contn->next = anot;
  101. printf("contn->next->word prints: %s\n",contn->next->word);
  102. //printf("contn->next prints: %s",contn->next);
  103.  
  104. for (int i = 0; greet[i] != '\0'; i++)
  105. {
  106. anot->word[i] = greet[i];
  107. }
  108.  
  109. printf("contn->next->word prints: %s\n",contn->next->word);
  110.  
  111. printf("anot->word prints: %s\n",anot->word);
  112. anot->next = dog;
  113.  
  114. printf("dog->next->word prints: %s\n",dog->next->word);
  115.  
  116. printf("animal[] = \n");
  117. for (int i = 0; animal[i] != '\0'; i++)
  118. {
  119. printf("animal[%i] = %c",i,animal[i]);
  120. printf("\n");
  121. }
  122.  
  123. printf("dog prints: ");
  124. for (int i = 0; dog->word[i] != '\0'; i++)
  125. {
  126. printf("%c",dog->word[i]);
  127. }
  128. printf("\n");
  129.  
  130. printf("anot prints: ");
  131. for (int i = 0; anot->word[i] != '\0'; i++)
  132. {
  133. printf("%c",anot->word[i]);
  134. }
  135. printf("\n");
  136.  
  137. node *table[N];// an array of nodes with N elements
  138.  
  139. table[0]->word[0] = 'c';
  140. table[0]->word[1] = 'h';
  141. table[0]->word[2] = 'a';
  142. table[0]->word[3] = 'n';
  143. table[0]->word[4] = 'g';
  144. table[0]->word[5] = 'e';
  145. table[0]->next = table[1];
  146. //->word[1];
  147.  
  148. table[1]->word[0] = 'f';
  149. table[1]->word[1] = 'o';
  150. table[1]->word[2] = 'r';
  151. table[1]->word[3] = 'g';
  152. table[1]->word[4] = 'e';
  153. table[1]->next = table[2];
  154.  
  155. table[2]->word[0] = 's';
  156. table[2]->word[1] = 'e';
  157. table[2]->word[2] = 'b';
  158. table[2]->word[3] = 'u';
  159. table[2]->word[4] = 'm';
  160. table[2]->next = table[0];
  161. //->word[0];
  162.  
  163.  
  164. for (int i = 0; table[0]->word[i] != '\0'; i++)
  165. {
  166. printf("table[0]->word[%i] = %c\n",i,table[0]->word[i]);
  167. }
  168.  
  169.  
  170. free(contn);
  171. free(dog);
  172. free(anot);
  173. }
  174. /*
  175.  
  176. printf("poodle->next->word prints:\n");
  177. for (int i = 0; poodle->next->word[i] != '\0'; i++)
  178. {
  179. printf("%c",poodle->word->next->word[i]);
  180. }
  181.  
  182.  
  183.  
  184. printf("the word stored in the test node's word field is %s\n",test->word);
  185. printf("the word stored in the contn node's word field is %s\n",contn->word);
  186. printf("the word stored in the anot node's word field is %s\n",anot->word);
  187.  
  188. printf("the word stored in the test node's next field is %s\n",test->word);
  189. printf("the word stored in the contn node's next field is %s\n",contn->word);
  190. printf("the word stored in the anot node's next field is %s\n",anot->word);
  191.  
  192.  
  193.  
  194. printf("the chacter at name[0] is: %c\n",name[0]); this is correct
  195. printf("the word at name prints: %s\n",name); this is correct
  196. printf("the hash value of the character t is: %i\n",hasht); this is correct
  197. printf("the hash value of the character t is: %i\n",hash(t)); this is correct
  198. printf("hash(name[0] prints %i)",hash(name[0])); ... this method doesn't work
  199. printf("the hashvalue of name[0] is %i\n",hash(&name[0]));
  200. this is not correct... prints 13
  201. printf("the hashvalue of name[1] is %i\n",hash(&name[1]));
  202. should hash to 13... incorrect
  203.  
  204. char nz = name[0];
  205. printf("the character at name[0] is %c\n",nz);
  206. printf("the hashvalue of name[0] is %i\n",hash(&nz));// this is correct
  207.  
  208. printf("the hash value of name... stored in the integer namval is: %i\n",namval);
  209. printf("the hash value of the character a is: %i\n",hasha);// this is correct
  210. printf("the hash value of the character a is: %i\n",hash(a));//this is correct
  211. printf("the hash value of the character b is: %i\n",hash("b"));//why double quotes needed?
  212. printf("the hash value of the character t is: %i\n",hash("t"));// t is hashing correctly here
  213.  
  214. printf("word[1] prints %c\n",word[1]);
  215.  
  216. char wo = word[1];
  217. 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
  218. printf("the hash value of word[1] is: %i\n",hash(&wo));
  219. printf("the hash value of wval is: %i\n\n",wval);
  220.  
  221.  
  222. printf("the hashvalue of alph[0] is %i\n",hash(&alph[0]));
  223. this does not work
  224.  
  225. what is this doing?... why does it return 13
  226.  
  227. printf("the hashvalue of alph[0] is %i\n",hash(&alp));// dereferencing the pointer?
  228.  
  229. printf("the hashvalue of a is %i\n",hasha);// this is correct
  230. printf("the hashvalue of a, or hash(a) is %i\n",hash(a));// these are correct
  231. printf("the hashvalue of t is %i\n",hash(t));
  232.  
  233.  
  234. for (int i = 0; alph[i] != '\0'; i++)
  235. {
  236. char c = alph[i];
  237. int alphash = hash(&c);
  238. //int alphash = hash(c);
  239.  
  240. //why&?
  241. printf("alph[%i] = %c\n it's hash value is %i\n",i,alph[i],alphash);// this is weird
  242. }
  243.  
  244. for (int i = 0; ALPH[i] != '\0'; i++)
  245. {
  246. char c = ALPH[i];
  247. //int ALPHASH = hash(&c);
  248.  
  249. //printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],ALPHASH);
  250. printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],hash(&c));
  251.  
  252. }
  253. */
  254.  
  255.  
Advertisement
Add Comment
Please, Sign In to add comment