Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. /**
  2. * dictionary.c
  3. *
  4. * Computer Science 50
  5. * Problem Set 5
  6. *
  7. * Implements a dictionary's functionality.
  8. */
  9.  
  10. #include <stdbool.h>
  11.  
  12. #include "dictionary.h"
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17.  
  18.  
  19.  
  20. //alpha + "'"
  21. #define alphabet 27
  22.  
  23. //create nodes
  24. typedef struct _node
  25. {
  26. bool is_word;
  27. struct _node* children[alphabet];
  28. }
  29. node;
  30.  
  31. void freenode(node* leaf);
  32.  
  33. node* root;
  34.  
  35. unsigned int dic_size = 0;
  36.  
  37. /**
  38. * Returns true if word is in dictionary else false.
  39. */
  40. bool check(const char* word)
  41. {
  42.  
  43. node* leaf = root;
  44.  
  45. for (int i = 0, n = strlen(word); i < n; i++)
  46. {
  47.  
  48. int alphaplace2 = tolower(word[i]) - 'a';
  49.  
  50. if (word[i] == '\'')
  51. {
  52. alphaplace2 = 26;
  53. }
  54.  
  55. if (leaf->children[alphaplace2] != NULL)
  56. {
  57.  
  58. leaf = leaf->children[alphaplace2];
  59.  
  60. if (leaf->is_word == true)
  61. {
  62. return true;
  63. }
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
  70.  
  71. return false;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement