joseleeph

Untitled

Oct 19th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 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] = NULL;
  138. node *table[N];
  139. // table is not initialized
  140. // an array of nodes with N elements
  141.  
  142. table[0]->word[0] = 'c';
  143. table[0]->word[1] = 'h';
  144. table[0]->word[2] = 'a';
  145. table[0]->word[3] = 'n';
  146. table[0]->word[4] = 'g';
  147. table[0]->word[5] = 'e';
  148. table[0]->word[6] = '\0';
  149.  
  150. table[0]->next = table[1];
  151. //->word[1];
  152.  
  153. table[1]->word[0] = 'f';
  154. table[1]->word[1] = 'o';
  155. table[1]->word[2] = 'r';
  156. table[1]->word[3] = 'g';
  157. table[1]->word[4] = 'e';
  158. table[1]->word[5] = '\0';
  159. table[1]->next = table[2];
  160.  
  161. table[2]->word[0] = 's';
  162. table[2]->word[1] = 'e';
  163. table[2]->word[2] = 'b';
  164. table[2]->word[3] = 'u';
  165. table[2]->word[4] = 'm';
  166. table[2]->word[5] = '\0';
  167. table[2]->next = table[0];
  168. //->word[0];
  169.  
  170.  
  171. for (int i = 0; table[0]->word[i] != '\0'; i++)
  172. {
  173. node *modnod = malloc(sizeof(node));
  174. printf("table[0]->word[%i] = %c\n",i,table[0]->word[i]);
  175. }
  176.  
  177.  
  178. free(contn);
  179. free(dog);
  180. free(anot);
  181. }
  182.  
  183.  
Advertisement
Add Comment
Please, Sign In to add comment