Search code examples
crecursionsolveranagram

Recursion problems in C


I am working on an anagram solver in C. Hit a problem where the solver will return the first few anagrams correctly, however on ones that extend past 2 words, it begins to enter an infinite loop.

Example:

I enter "team sale rest" into the anagram solver, it responds with teamster ale, and a few others. Then when it arrives at releases, it enters an infinite loop where it prints "releases am matt" "releases am am matt" etc.

Here is the code base:

//recursively find matches for each sub-word
int findMatches(char string[], char found_so_far[])
{
    printf("String entering function: %s\n", string);
    int string_length = strlen(string);
    int_char_ptr *results = getPowerSet(string, string_length);
    if(!results)
        return 2;
    // selects length of subset, starting with the largest
    for (int i = string_length - 1; i > 0; i--)
    {
        // iterates through all the subsets of a particular length
        for(int j = 0; j < results->count[i]; j++)
        {
            word_array *matches = NULL;
            // check words against dictionary
            matches = dictionary_check(results->table[i][j]);
            if (matches)
            {
                // iterate through matches
                for(size_t k = 0; k < matches->size; k++)
                {
                    int found_length;
                    // find out length of string needed for found
                    if (strcmp(found_so_far, "") == 0)
                        found_length = strlen(matches->arr[k]) + 1;
                    else
                        found_length = strlen(found_so_far) + strlen(matches->arr[k]) + 2;
                    char found[found_length];

                    // on first passthrough, copy directly from matches
                    if (strcmp(found_so_far, "") == 0)
                        strcpy(found, matches->arr[k]);
                    else
                        sprintf(found, "%s %s", found_so_far, matches->arr[k]);
                    char tempstr[string_length];
                    strcpy(tempstr, string);
                    char *remain = get_remaining_letters(tempstr, results->table[i][j]);
                    // if there are no letters remaining
                    if (strcmp(remain, "") == 0)
                    {
                        printf("MATCH FOUND: %s \n", found);
                        // alternatively, could store strings to array
                    }
                    else
                    {
                        findMatches(remain, found);
                    }
                }
            }
        }
        free(results->table[i][results->count[i] - 1]);
        free(results->table[i]);
    }
    return 0;
}

How I read it (I am obviously missing something) is that it should try to match all matches, and if it can't , it should move to the next subset of letters found.

I have tries going through with a debugger, and cant make rhyme or reason of it.


Solution

  • As mentioned above in the commment:

    get_remaining_letters used the original results->table[i][j] and removed the letters. This would leave an empty string for the next iteration and cause it to not perform as expected. Fixed by copying the string to a temporary one inside that function.