The function is given a string (for example "communism") and checks whether or not its suffix is a part of a wordbank - the program takes an absurdly long time (or is infinite-looped, I don't know but I suspect the first). here's an example of the suffix wordbank (reversed) char* noun_suffixes[] = { "msi", "re", "sci", "ssen", "tnem", "tsi" }; Here's the function: (n is the number of words in the bank, *str is the word).
bool is_suffix_in_dict(char* str, char* dict[], int n)
{
printf("first checkpoint reached");
char strRev[MAX_LEN + 1] = { 0 }, strToCheck[MAX_LEN + 1] = { 0 };
unsigned int len = strlen(str);
for (unsigned int i = 0; i < len; i++)
{
strRev[len - i] = *(str + i);
}
for (unsigned int i = 0, j = 0; i < len; i++)
{
while (strRev[i] < 'a' || strRev[i] > 'z')
{
i++;
}
if (i < len)
{
strToCheck[j] = strRev[i];
if (binarySearch(dict, n, strToCheck))
{
printf("second check reached - positive");
return TRUE;
}
j++;
}
}
printf("second check reached - neg");
return FALSE;
}
the function calls upon this one-
bool binarySearch(char* dict[], int n, char s[MAX_LEN + 1])
{
int last = n, first = 0, mid = n / 2;
while (first < n)
{
int cmpVal = strcmp(dict[mid], s);
if (cmpVal < 0)
{
printf("cmpVal<0");
first = mid + 1;
mid = (first + last) / 2;
}
else if (cmpVal > 0)
{
printf("cmpVal>0");
last = mid;
mid = (first + last) / 2;
}
else
{
return TRUE;
}
}
return FALSE;
}