Skip to content

Instantly share code, notes, and snippets.

/dictionary.c Secret

Created April 3, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/512019988591d030e54cd5a671837fed to your computer and use it in GitHub Desktop.
Save anonymous/512019988591d030e54cd5a671837fed to your computer and use it in GitHub Desktop.
dictionary.c - shared from CS50 IDE
/**
* 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