joseleeph

Untitled

Oct 19th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 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]->word[6] = '\0';
  146.  
  147. table[0]->next = table[1];
  148. //->word[1];
  149.  
  150. table[1]->word[0] = 'f';
  151. table[1]->word[1] = 'o';
  152. table[1]->word[2] = 'r';
  153. table[1]->word[3] = 'g';
  154. table[1]->word[4] = 'e';
  155. table[1]->word[5] = '\0';
  156. table[1]->next = table[2];
  157.  
  158. table[2]->word[0] = 's';
  159. table[2]->word[1] = 'e';
  160. table[2]->word[2] = 'b';
  161. table[2]->word[3] = 'u';
  162. table[2]->word[4] = 'm';
  163. table[2]->word[5] = '\0';
  164. table[2]->next = table[0];
  165. //->word[0];
  166.  
  167.  
  168. for (int i = 0; table[0]->word[i] != '\0'; i++)
  169. {
  170. printf("table[0]->word[%i] = %c\n",i,table[0]->word[i]);
  171. }
  172.  
  173.  
  174. free(contn);
  175. free(dog);
  176. free(anot);
  177. }
  178. /*
  179.  
  180. printf("poodle->next->word prints:\n");
  181. for (int i = 0; poodle->next->word[i] != '\0'; i++)
  182. {
  183. printf("%c",poodle->word->next->word[i]);
  184. }
  185.  
  186.  
  187.  
  188. printf("the word stored in the test node's word field is %s\n",test->word);
  189. printf("the word stored in the contn node's word field is %s\n",contn->word);
  190. printf("the word stored in the anot node's word field is %s\n",anot->word);
  191.  
  192. printf("the word stored in the test node's next field is %s\n",test->word);
  193. printf("the word stored in the contn node's next field is %s\n",contn->word);
  194. printf("the word stored in the anot node's next field is %s\n",anot->word);
  195.  
  196.  
  197.  
  198. printf("the chacter at name[0] is: %c\n",name[0]); this is correct
  199. printf("the word at name prints: %s\n",name); this is correct
  200. printf("the hash value of the character t is: %i\n",hasht); this is correct
  201. printf("the hash value of the character t is: %i\n",hash(t)); this is correct
  202. printf("hash(name[0] prints %i)",hash(name[0])); ... this method doesn't work
  203. printf("the hashvalue of name[0] is %i\n",hash(&name[0]));
  204. this is not correct... prints 13
  205. printf("the hashvalue of name[1] is %i\n",hash(&name[1]));
  206. should hash to 13... incorrect
  207.  
  208. char nz = name[0];
  209. printf("the character at name[0] is %c\n",nz);
  210. printf("the hashvalue of name[0] is %i\n",hash(&nz));// this is correct
  211.  
  212. printf("the hash value of name... stored in the integer namval is: %i\n",namval);
  213. printf("the hash value of the character a is: %i\n",hasha);// this is correct
  214. printf("the hash value of the character a is: %i\n",hash(a));//this is correct
  215. printf("the hash value of the character b is: %i\n",hash("b"));//why double quotes needed?
  216. printf("the hash value of the character t is: %i\n",hash("t"));// t is hashing correctly here
  217.  
  218. printf("word[1] prints %c\n",word[1]);
  219.  
  220. char wo = word[1];
  221. 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
  222. printf("the hash value of word[1] is: %i\n",hash(&wo));
  223. printf("the hash value of wval is: %i\n\n",wval);
  224.  
  225.  
  226. printf("the hashvalue of alph[0] is %i\n",hash(&alph[0]));
  227. this does not work
  228.  
  229. what is this doing?... why does it return 13
  230.  
  231. printf("the hashvalue of alph[0] is %i\n",hash(&alp));// dereferencing the pointer?
  232.  
  233. printf("the hashvalue of a is %i\n",hasha);// this is correct
  234. printf("the hashvalue of a, or hash(a) is %i\n",hash(a));// these are correct
  235. printf("the hashvalue of t is %i\n",hash(t));
  236.  
  237.  
  238. for (int i = 0; alph[i] != '\0'; i++)
  239. {
  240. char c = alph[i];
  241. int alphash = hash(&c);
  242. //int alphash = hash(c);
  243.  
  244. //why&?
  245. printf("alph[%i] = %c\n it's hash value is %i\n",i,alph[i],alphash);// this is weird
  246. }
  247.  
  248. for (int i = 0; ALPH[i] != '\0'; i++)
  249. {
  250. char c = ALPH[i];
  251. //int ALPHASH = hash(&c);
  252.  
  253. //printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],ALPHASH);
  254. printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],hash(&c));
  255.  
  256. }
  257. */
  258.  
  259.  
Advertisement
Add Comment
Please, Sign In to add comment