joseleeph

Untitled

Oct 8th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 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.  
  15.  
  16.  
  17. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  18. {
  19. int val = 0;
  20. //int cval;
  21. for (int i = 0; word[i] != '\0'; i++)
  22. {
  23. //val += word[i];
  24.  
  25. if (isalpha(word[i]))
  26. {
  27. if (isupper(word[i]))
  28. {
  29. val += word[i] - 65;
  30. }
  31. if (islower(word[i]))
  32. {
  33. val += word[i] - 97;
  34. }
  35. if (word[i] == 13)
  36. {
  37. continue;
  38. }
  39. }
  40. else
  41. continue;
  42.  
  43. }
  44. // TODO
  45. return val%26;
  46. // maybe try a hash table that will use the first 3 letters
  47.  
  48. //return 0;
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. char *word = "caramel";
  53. char *name = "tommy";
  54. char *a = "a";
  55. char *t = "t";
  56. char *alph = "abcdefghijklmnopqrstuvwxyz";
  57. char *ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  58. int val = 0;
  59. int namval = hash(name);
  60. int wval = hash(word);
  61. int hasha = hash(a);
  62. int hasht = hash(t);
  63. node *test;
  64. node *contn;
  65. test->word = "hello";
  66. contn->word = "sir";
  67. test->next = contn->word;
  68. node *table[N];
  69. printf("the word stored in the test node's word field is %s\n",test->word);
  70.  
  71.  
  72. printf("the chacter at name[0] is: %c\n",name[0]);//this is correct
  73. printf("the word at name prints: %s\n",name);// this is correct
  74. printf("the hash value of the character t is: %i\n",hasht);// this is correct
  75. printf("the hash value of the character t is: %i\n",hash(t));// this is correct
  76. //printf("hash(name[0] prints %i)",hash(name[0])); ... this method doesn't work
  77. printf("the hashvalue of name[0] is %i\n",hash(&name[0]));// this is not correct... prints 13
  78. printf("the hashvalue of name[1] is %i\n",hash(&name[1]));// o should hash to 13... incorrect
  79. printf("the hash value of name... stored in the integer namval is: %i\n",namval);
  80. printf("the hash value of the character a is: %i\n",hasha);// this is correct
  81. printf("the hash value of the character a is: %i\n",hash(a));//this is correct
  82. printf("the hash value of the character b is: %i\n",hash("b"));//why double quotes needed?
  83. printf("the hash value of the character t is: %i\n",hash("t"));// t is hashing correctly here
  84.  
  85. 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
  86. printf("the hash value of wval is: %i\n\n",wval);
  87.  
  88. printf("the character at alph[0] is %c\n",alph[0]);
  89. printf("the hashvalue of alph[0] is %i\n",hash(&alph[0]));// what is this doing?
  90. printf("the hashvalue of a is %i\n",hasha);// this is correct
  91. printf("the hashvalue of a, or hash(a) is %i\n",hash(a));// these are correct
  92. printf("the hashvalue of t is %i\n",hash(t));
  93. for (int i = 0; alph[i] != '\0'; i++)
  94. {
  95. char c = alph[i];
  96. int alphash = hash(&c);
  97. //int alphash = hash(c);
  98.  
  99. //why&?
  100. printf("alph[%i] = %c\n it's hash value is %i\n",i,alph[i],alphash);// this is weird
  101. }
  102.  
  103. for (int i = 0; ALPH[i] != '\0'; i++)
  104. {
  105. char c = ALPH[i];
  106.  
  107. printf("ALPH[%i] = %c\n it's hash value is %i\n",i,ALPH[i],hash(&c));
  108.  
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment