Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 30
void RecursivePermute(char jumble[], int k, char * words[], int size_of_dictionary);
void ExchangeCharacters(char jumble[], int i, int j);
int inDictionary(char jumble[], char * words[], int size_of_dictionary);
int main() {
FILE * ifp;
int size_of_dictionary, num_of_jumbled_words;
int i, j;
char ** words;
char jumble[MAX_SIZE];
ifp = fopen("dictionary.in", "r");
if(ifp == NULL) {
printf("The file that you are trying to access is invalid.\n");
}
else {
fscanf(ifp, "%d", &size_of_dictionary);
}
words = (char **)malloc(size_of_dictionary*sizeof(char*));
for(i=0; i<size_of_dictionary; i++) {
if(words == NULL) {
printf("Sorry, there is not enough available memory to be allocated.\n");
break;
}
words[i] = (char*)malloc(sizeof(char)*MAX_SIZE);
fscanf(ifp, "%s", &words[i]);
}
fclose(ifp);
ifp = fopen("jumble.txt", "r");
fscanf(ifp, "%d", &num_of_jumbled_words);
for(j=1; j<=num_of_jumbled_words; j++) {
printf("Jumble #%d:\n", j);
fscanf(ifp, "%s", &jumble);
RecursivePermute(jumble, 0, words, size_of_dictionary);
}
fclose(ifp);
free(words);
return 0;
}
// Pre-condition: 'words' is a valid C String, and 'k' is non-negative and
// less than or equal to the length of words.
// Post-condition: All of the permutations of words with the first 'k'
// characters fixed in their original positions are
// printed. Namely, if n is the length of words, then
// (n-k)! permutations are printed.
void RecursivePermute(char jumble[], int k, char * words[], int size_of_dictionary) {
int i, j;
// Base-case: Since all letters are fixed, we can ONLY print
// what's stored in str.
if (k == strlen(jumble)) {
if(inDictionary(jumble, words, size_of_dictionary) == 1) {
printf("A permutation of %s that is a valid word is .\n", jumble, words[inDictionary(jumble, words, size_of_dictionary)]);
}
}
else {
// Loop through each possible starting letter for index k,
// the first index for which we have a choice.
for (j=k; j<strlen(jumble); j++) {
// Place the character stored in index j in location k.
ExchangeCharacters(jumble, k, j);
// Print out all of the permutations with that character
// just chosen above fixed.
RecursivePermute(jumble, k+1, words, size_of_dictionary);
// Put the original character that used to be there back
// in its place.
ExchangeCharacters(jumble, j, k);
}
}
}
// Pre-condition: words is a valid C String and i and j are valid indexes
// to that string.
// Post-condition: The characters at index i and j will be swapped in
// str.
void ExchangeCharacters(char jumble[], int i, int j) {
char temp = jumble[i];
jumble[i] = jumble[j];
jumble[j] = temp;
}
int inDictionary(char jumble[], char * words[], int size_of_dictionary) {
int left=0, mid, right=size_of_dictionary-1;
while(left <= right) {
mid = (left + right)/2;
if((strcmp(jumble, words[mid])) == 0) {
return mid;
}
if((strcmp(jumble, words[mid])) < 0) {
left = mid + 1;
}
if((strcmp(jumble, words[mid])) > 0) {
right = mid - 1;
}
}
return 0;
}