-
-
Save anonymous/512019988591d030e54cd5a671837fed to your computer and use it in GitHub Desktop.
dictionary.c - shared from CS50 IDE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns true if word is in dictionary else false. | |
*/ | |
bool check(const char *word) | |
{ | |
// starting the temp pointer at the root of the trie | |
temp = root; | |
// iterating over the length of the passed in words | |
for (int i = 0; i < strlen(word) + 1 ; i ++) | |
{ | |
/** converting chars upper to lower to preserve case sensitivity | |
* and skipping assignment to 0 or 10 to avoid conflict with | |
* '/0' and '/n' chars | |
*/ | |
int x = word[i]; | |
if (isupper(word[i])) | |
{ | |
x = tolower(word[i]); | |
x = x - 96; | |
if (x == 10) | |
{ | |
x = 27; | |
} | |
} | |
else if (islower(word[i])) | |
{ | |
x = word[i] - 96; | |
if (x == 10) | |
{ | |
x = 27; | |
} | |
} | |
else if (word[i] == '\'') | |
{ | |
x = 28; | |
} | |
// if corresponding node is NULL | |
if (temp->paths[x] == NULL) | |
{ | |
// check to see if we are at the end of our passed in word | |
if (x == '\0') | |
{ | |
// if valid word this is a word in dictionary | |
if (temp->validword == true) | |
{ | |
return true; | |
} | |
//otherwise not a word in dictionary | |
else | |
{ | |
return false; | |
} | |
} | |
// otherwise we're in the middle of a word and can't proceed | |
else | |
{ | |
// not a word | |
return false; | |
} | |
} | |
// the corresponding node is open | |
else if (temp->paths[x] != NULL) | |
{ | |
//proceed to the next node | |
temp = temp -> paths[x]; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment