Advertisement
joseleeph

Untitled

Oct 10th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 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. char *breed = "poodle";
  57. int val = 0;
  58. int namval = hash(name);
  59. int wval = hash(word);
  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 word is %i\n",wval);
  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. printf("dog->next->word prints: %s\n",dog->next->word);
  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. printf("anot->word prints: %s\n",anot->word);
  109. anot->next = dog;
  110.  
  111. printf("word[] = ");
  112. for (int i = 0; word[i] != '\0'; i++)
  113. {
  114. printf("word[%i] = %c",i,word[i]);
  115. printf("\n");
  116. }
  117.  
  118. printf("dog prints: ");
  119. for (int i = 0; dog->word[i] != '\0'; i++)
  120. {
  121. printf("%c",dog->word[i]);
  122. }
  123. printf("\n");
  124.  
  125. printf("anot prints: ");
  126. for (int i = 0; anot->word[i] != '\0'; i++)
  127. {
  128. printf("%c",anot->word[i]);
  129. }
  130. printf("\n");
  131.  
  132. node *table[N];// an array of nodes with N elements
  133. free(contn);
  134. free(dog);
  135. free(anot);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement