joseleeph

Untitled

Oct 8th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 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. node *table[N];
  15.  
  16. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  17. {
  18. int val = 0;
  19. //int cval;
  20. for (int i = 0; word[i] != '\0'; i++)
  21. {
  22. //val += word[i];
  23.  
  24. if (isalpha(word[i]))
  25. {
  26. if (isupper(word[i]))
  27. {
  28. val += word[i] - 65;
  29. }
  30. if (islower(word[i]))
  31. {
  32. val += word[i] - 97;
  33. }
  34. if (word[i] == 13)
  35. {
  36. continue;
  37. }
  38. }
  39. //else
  40. //val += word[i];
  41.  
  42. }
  43. // TODO
  44. return val%26;
  45. // maybe try a hash table that will use the first 3 letters
  46.  
  47. //return 0;
  48. }
  49. int main(int argc, char *argv[])
  50. {
  51. char *word = "caramel";
  52. char *name = "tommy";
  53. char *a = "a";
  54. char *t = "t";
  55. char *alph = "abcdefghijklmnopqrstuvwxyz";
  56. char *ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  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.  
  63.  
  64. printf("the chacter at name[0] is: %c\n",name[0]);//this is correct
  65. printf("the word at name prints: %s\n",name);// this is correct
  66. printf("the hash value of the character t is: %i\n",hasht);// this is correct
  67. printf("the hash value of the character t is: %i\n",hash(t));// this is correct
  68. //printf("hash(name[0] prints %i)",hash(name[0])); ... this method doesn't work
  69. printf("the hashvalue of name[0] is %i\n",hash(&name[0]));// this is not correct... prints 13
  70. printf("the hashvalue of name[1] is %i\n",hash(&name[1]));// o should hash to 13... incorrect
  71. printf("the hash value of name... stored in the integer namval is: %i\n",namval);
  72. printf("the hash value of the character a is: %i\n",hasha);// this is correct
  73. printf("the hash value of the character a is: %i\n",hash(a));//this is correct
  74. printf("the hash value of the character b is: %i\n",hash("b"));//why double quotes needed?
  75. printf("the hash value of the character t is: %i\n",hash("t"));// t is hashing correctly here
  76.  
  77. 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
  78. printf("the hash value of wval is: %i\n\n",wval);
  79.  
  80. printf("the character at alph[0] is %c\n",alph[0]);
  81. printf("the hashvalue of alph[0] is %i\n",hash(&alph[0]));// what is this doing?
  82. printf("the hashvalue of a is %i\n",hasha);// this is correct
  83. printf("the hashvalue of a, or hash(a) is %i\n",hash(a));// these are correct
  84. printf("the hashvalue of t is %i\n",hash(t));
  85. for (int i = 0; alph[i] != '\0'; i++)
  86. {
  87. char c = alph[i];
  88. int alphash = hash(&c);
  89. //int alphash = hash(c);
  90.  
  91. //why&?
  92. printf("alph[%i] = %c\n it's hash value is %i\n",i,alph[i],alphash);// this is weird
  93. }
  94.  
  95. for (int i = 0; ALPH[i] != '\0'; i++)
  96. {
  97. char c = ALPH[i];
  98. //int alphash = hash(&c);
  99. //int alphash = hash(c);
  100.  
  101. //why&?
  102. printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],hash(&c));
  103. printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],hash(&ALPH[i]));
  104. }
  105.  
  106.  
  107.  
  108. //int cval;
  109. /*
  110. for (int i = 0; word[i] != '\0'; i++)
  111. {
  112. if (isalpha(word[i]))
  113. {
  114. if (isupper(word[i]))
  115. {
  116. printf("word[%i] = %c with a hash value of %i\n",i,word[i],word[i] - 65);
  117. val += word[i] - 65;
  118. }
  119. if (islower(word[i]))
  120. {
  121. printf("word[%i] = %i\n",i,word[i] - 97);
  122. val += word[i] - 97;
  123.  
  124. }
  125.  
  126. }
  127. else
  128. {
  129. val += word[i];
  130. }
  131. printf("value is now %i\n", val);
  132. }
  133. for (int i = 0; alph[i] != '\0'; i++)
  134. {
  135. if (isupper(alph[i]))
  136. {
  137. printf("alph[%i] = %i\n",i,alph[i]-65);
  138. val += alph[i] - 65;
  139. }
  140. if (islower(alph[i]))
  141. {
  142. printf("alph[%i] = %i\n",i,alph[i]-97);
  143. val += alph[i] - 97;
  144.  
  145. }
  146. }
  147. for (int i = 0; ALPH[i] != '\0'; i++)
  148. {
  149. if (isupper(ALPH[i]))
  150. {
  151. printf("ALPH[%i] = %i\n",i,ALPH[i]-65);
  152. val += ALPH[i] - 65;
  153. }
  154. if (islower(ALPH[i]))
  155. {
  156. printf("ALPH[%i] = %i\n",i,ALPH[i]-97);
  157. val += ALPH[i] - 97;
  158.  
  159. }
  160. }
  161. */
  162.  
  163. // TODO
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment