Hello.
I'm making a small application to make permutations of strings in order to find any anagrams. These permutations are then checked against a dictionary file using fgets. It compiles nicely but when it is run it just crashes.
Here is the entire source code but I suspect only the "checkDict" function is faulty because everything runs just fine when I comment it out.
Code:#include <stdio.h> #define VERSION 0.7 #define MAX_DICT_LEN 100 #define MAX_RESULTS 10 int maxResultsCounter = 0; char *temp; FILE *dict; // check current permutation against entire dict.txt void checkDict(char *str){ while(fgets(temp, MAX_DICT_LEN, dict) != NULL) if(strcmp(str, temp) == 0){ printf("%s\n", str); maxResultsCounter++; } } // swap *first and *second void swap(char* first, char* second){ char temp = *second; *second = *first; *first = temp; } // compute permutations int perm(char* set, int begin, int end){ int i, c = 0, range = end - begin; if(range == 1){ // MAX_RESULTS invariant if(maxResultsCounter > MAX_RESULTS) return 0; //checkDict(set); } else for(i = 0; i < range; i++){ swap(&set[begin], &set[begin+i]); // first swap perm(set, begin+1, end); // recursion swap(&set[begin], &set[begin+i]); // swap back } return 0; } int main(int argc, char *argv[]){ // test arguments if(argc == 1 || argc > 3){ printf("AnagramAnalyzer (v. %2.1f)\n\n", VERSION); printf("syntax: \n\t* <anagram> <args>\n\n"); printf("args: \n\t[-s] adds a space to the anagram\n"); return 0; } char *in = argv[1]; // convert to lower case int i, len = strlen(in); for(i = 0; i < len; i++) if(in[i] > 64 && in[i] < 91) in[i] += 32; // check for additional arguments if(argc == 3 && strcmp(argv[2], "-s") == 0){ in[len] = ' '; in[len+1] = '\0'; len++; } // open file stream if((dict = fopen("dict.txt", "r")) == NULL){ printf("Couldn't locate dict file!\n"); return 0; } // permutate and check dictionary printf("Checking...\n"); perm(in, 0, len); // stop and close fclose(dict); return 0; }


