======Project: Jumble Solver====== A project for Data Structures and Systems Programming by Brad Hammond during the Fall of 2011. =====Objectives===== State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it? =====Prerequisites===== In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved: * Knowledge of pointers and command line arguments * Wikipedia/Algorithms in a Nutshell book for Data Structures * Knowledge of file I/O =====Background===== Ever wanted to be able to solve the word jumble puzzles in the newspaper really fast? Or beat your friends at scrabble after they laugh at you for being so bad at it? The purpose of this program is to accept n jumbled words and unscramble them. Through writing this we will achieve a better knowledge of file I/O, pointers, sorting, and using command line arguments. =====Scope===== File access is an important concept to be familiar with. In addition to regular files, being able to deal with directories is especially important. This project will implement our own version of **ls** which can be used to list files in directories on the system. It will do so by manipulating directory files and accessing their contents, and displaying the information in a readable form to STDOUT. =====Attributes===== State and justify the attributes you'd like to receive upon successful approval and completion of this project. * files: files will be manipulated in this project * command line arguments: command line arguments will be used to get input * pointers: pointers to various strings and files will be used in this project * malloc/free: used to allocate/deallocate various memory for strings * sort: selection sort used to sort strings =====Code===== #include #include #include #include /***************************************** * Anagram solver * * Program to accept an arbitray number * * of strings as input and will find any* * anagrams within /usr/share/dict/words* ******************************************/ // Error Codes: Error code 1 indicates the user // did not enter any arguments at the command line. // Error code 2 indicates the input file could not // be found #define NO_ARG 1 #define NO_FILE 2 #define WORD_LEN 64 #define MIN_ARGS 2 FILE *openFile(void); void lowerCase(char*, const int); void sortWord(char*, const int); int compareWords(char*, char*); int main(int argc, char** argv) { /* ******************Variable Declarations******************** * wordList - dictionary list used as input * start - Stores the beginning position of the file * used to return to the beginning of the list list * everytime we solve for a new jumble * argPos - Index used to ascend through the CL arguments * dictWord - stores each word from the dictionary file * jumble - copy of the current argument * solved - unsorted solution to the current argument */ FILE *wordList; fpos_t start; int argPos = 0; char *dictWord = (char*)malloc(sizeof(char) * WORD_LEN); char *jumble = (char*)malloc(sizeof(char) * WORD_LEN); char *solved = (char*)malloc(sizeof(char) * WORD_LEN); //Check argument count. Exit if < 2 if (argc < MIN_ARGS) { printf("error: enter at least one argument\n"); exit(NO_ARG); } //Check if file exists. Exit if not if ((wordList = openFile()) == NULL) { printf("error: could not find word list. save word list"); printf(" 'words.txt' to current directory.\n"); exit(NO_FILE); } //Sets the file position to the beginning of the file fgetpos(wordList, &start); /* * Check if argPos is < argc and if so copy the contents * of the current argument into jumble. Convert any * capital letters to lowercase and then sort the word * alphabetically. * * Check if the list is at the end of the file, if not * store the next word in the list into dictWord. Then * copy the contents of dictWord into solved and convert * solved to lowercase. Now we compare jumble and dictword * and if after being sorted they are both the same then * we have found a solution which will be printed to the * screen. */ while (++argPos < argc) { strcpy(jumble, argv[argPos]); printf("Solutions for %s: ", jumble); lowerCase(jumble, strlen(jumble)); sortWord(jumble, strlen(jumble)); while (feof(wordList) == 0) { fscanf(wordList, "%s", dictWord); strcpy(solved, dictWord); lowerCase(solved, strlen(solved)); if (compareWords(jumble, dictWord) == 0) printf("%s ", solved); } printf("\n"); //return to the beginning of the file fsetpos(wordList, &start); } //Close the input file and free memory fclose(wordList); free(dictWord); free(jumble); free(solved); return 0; } FILE *openFile(void) { FILE *inputFile; if ((inputFile = fopen("/usr/share/dict/words", "r")) == NULL) if ((inputFile = fopen("words.txt", "r")) == NULL) return NULL; return inputFile; } void lowerCase(char* s, const int size) { for (int i = 0; i < size; i++) s[i] = tolower(s[i]); } void sortWord(char *s, const int size) { //Selection sort used to sort the string int minIdx, temp; for (int i = 0; i < size; i++) { minIdx = i; for (int j = i + 1; j < size; j++) { if (s[j] < s[minIdx]) minIdx = j; } if (minIdx != i) { temp = s[minIdx]; s[minIdx] = s[i]; s[i] = temp; } } } int compareWords(char *jumble, char *dictWord) { /* * As long as flag is set to 0 there is the * possibility of a solution. If after the * entire function executes flag is still 0, * then we have a solution. */ int flag = 0; /* * First check to see if the words are the same * length. If not, then they cannot be a match. * If they are we then convert the dictionary * word to lowercase and sort it. From there we do * a char by char comparison and if a mismatch is * found we break from the loop and set flag to -1. */ if (strlen(jumble) == strlen(dictWord)) { lowerCase(dictWord, strlen(dictWord)); sortWord(dictWord, strlen(dictWord)); for (int i = 0; i < strlen(jumble); i++) { if (jumble[i] != dictWord[i]) { flag = -1; break; } } } else flag = -1; return flag; } =====Execution===== Again, if there is associated code with the project, and you haven't already indicated how to run it, provide a sample run of your code: brad@Lucid-Lynx:/media/06F6-9DC2/Notepad++/Programs$ ./jumbleTwoPointO wot cta dki dhilc cark lnkide sitl manow pdaetno rekab sado sye on husc bjo pots okclc rdoo nomey arteh catfs meti viel rafe tno a nniaj moort ralenu gba cdoe lapy crede tetill meag becu Solutions for wot: tow two wot Solutions for cta: act cat Solutions for dki: kid Solutions for dhilc: child Solutions for cark: rack Solutions for lnkide: kilned kindle linked Solutions for sitl: list silt slit Solutions for manow: woman Solutions for pdaetno: notepad Solutions for rekab: baker baker brake break Solutions for sado: soda Solutions for sye: yes Solutions for on: no on Solutions for husc: such Solutions for bjo: job job Solutions for pots: post opts post pots spot stop tops Solutions for okclc: clock Solutions for rdoo: door odor rood Solutions for nomey: money Solutions for arteh: earth harte earth hater heart Solutions for catfs: facts Solutions for meti: emit item mite time Solutions for viel: levi evil live veil vile Solutions for rafe: fare fear Solutions for tno: not ton Solutions for a: a a Solutions for nniaj: jinan ninja Solutions for moort: motor Solutions for ralenu: lauren neural unreal Solutions for gba: bag gab Solutions for cdoe: code coed Solutions for lapy: play Solutions for crede: creed Solutions for tetill: little little Solutions for meag: game Solutions for becu: cebu cube =====Reflection===== Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project? I got stuck on this project in the beginning of the semester. I was having problems getting the word sorted out and ending up having more characters in the string that I wanted. I'd have to remove them and resort the word. All of this caused some issues. After walking away from it for most of the semester my job was made easier. New knowledge of things like fscanf and sorting algorithms helped greatly in the final product. I was able to use code from the keywords section for the sorting. I used a selection sort to sort the strings and get them in alphabetical order. I went from banging my head against a wall for hours to having the entire project done in a few hours. =====References===== In performing this project, the following resources were referenced: * C Pocket reference * My opus * Systems Programming: Guide to Unix/Linux Programming Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)