Thread: Another Jumble Program in C

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    12

    Another Jumble Program in C

    Hi everyone.

    So let me start off by saying that the code here has nothing to do with the code in my other post. I wrote the latter one completely wrong, so I started over with the one below.

    Now that iv'e realized what i'm being asked to do, I can correctly tell you guys what it is, I have to create a program that reads from a given list of dictionary words and from a list of given jumbled words. Then I have to compare the two, then print out what the jumbled word is and the actual word is. Not to hard of a program to construct in my opinion.

    Btw, the dictionary file (jumbledct.txt) and the jumbled file (jumble.txt) both have an integer at the top, which is the amount of words in each file. The jumbled file is just a sample of one of the many text files I plan to run with the program.

    Since I'm still a newbie at programming, Iv'e ran into another issue that I cant seem to figure out. I'm pretty sure I'm missing a few strcmp's. But my code isn't printing out correctly. Could someone please explain to me what I'm doing wrong?

    Thanks for your help.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAX_WORD_LEN 6
    #define MAX_NUM_WORDS 30000
    
    
    // Struct to use.
    struct jumble {
        char word[MAX_WORD_LEN+1];
        char sort[MAX_WORD_LEN+1];
    };
    
    
    void bubblesort(char letters[], int length) {
    
    
        char temp;
        int i, j;
    
    
        // sort the length array
        for(i = 0; i < length; i++){
            for(j = 0; j < length-i-1; j++){
                if(letters[j] > letters[j+1]){
                    temp = letters[j];
                    letters[j] = letters[j+1];
                    letters[j+1] = temp;
    
    
                }
            }
        }
    }
    
    
    int main() {
    
    
        struct jumble list[MAX_NUM_WORDS];
    
    
        int i, j;
    
    
        FILE* dictionary = fopen("jumbledct.txt", "r");
        FILE* jumbled = fopen("jumble.txt", "r");
    
    
        if ((dictionary == NULL) || (jumbled == NULL)){
            printf("Error, file does not exist.");
        }
    
    
        else{
    
    
            // Read in the number of words from the dictionary.
            int numwords;
            fscanf(dictionary, "%d", &numwords);
    
    
            int num_jumb;
            fscanf(jumbled, "%d", &num_jumb);
    
    
            // Go through each word.
            for (i = 1; i <= numwords; i++) {
                fscanf(dictionary, "%s", list[i].word);
    
    
                // Go through each jumbled word
                for (j = 1; j <= num_jumb; j++)
                    fscanf(jumbled, "%s", list[i].sort);
    
    
    
    
                    // Compare each word.
                    strcmp(list[i].sort, list[i].word);
    
    
                    //Initialize bubblesort array.
                    bubblesort(list[i].sort, strlen(list[i].word));
            }
    
    
            if (num_jumb < 100){
    
    
                // Print out the words and their other arrangement.
                for (i = 1; i <= num_jumb; i++){
                        printf("Jumble Puzzle #%d: %s\n", i, list[i].sort);
                        printf("%s\n", list[i].word);
                        printf("\n");
                        printf("\n");
                }
            }
    
    
            else{
                printf("Your number must be less than 100.\n");
            }
        }
    
    
        return 0;
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Previously discussed here.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C your arrays start with the index 0, not 1. The standard idiom is:
    Code:
    for(i=0;i<NumberOfWords;i++) 
       ... etc.
    using <= leads to errors, since you are 1 past the end of the array.

    In the inner for loop, you start off (correctly), using 'j' as your iterator. But then you only use 'i', again.

    Use i and j in the inner for loop. You should be comparing list[i].word with list[j].word, not with .sort.

    strcmp() returns the value of the comparison: 0 means equals, anything else means unequal. You need to use that return value from strcmp.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Does this look any better at all? I'm not very good at following the strcmp()'s. I guess I dont understand them very well. I pretty much have everything I need, except for the print-out of each of the words un-jumbled. I know I have to take the jumble.txt file and alphabetize those words by letter then compare those words to the dictionary words, and compare the two to see if they are == to 0 and if so I just run a print command of the words that 'checked out'. Did I say that right?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAX_WORD_LEN 6
    #define MAX_NUM_WORDS 30000
    
    
    // Struct to use.
    typedef struct {
        char word[MAX_WORD_LEN+1];
        char sort[MAX_WORD_LEN+1];
    } jumble_type;
    
    
    void bubblesort(char letters[], int length) {
    
    
        char temp;
        int i, j;
    
    
        // sort the length array
        for(i = 0; i < length; i++){
            for(j = i; j < length; j++){
                if(letters[i] > letters[j]){
                    temp = letters[j];
                    letters[j] = letters[i];
                    letters[i] = temp;
    
    
                }
            }
        }
    }
    
    
    int main() {
    
    
        jumble_type list[MAX_NUM_WORDS];
        jumble_type jumb[MAX_NUM_WORDS];
    
    
        int i, j, k, l;
    
    
        FILE* dictionary = fopen("jumbledct.txt", "r");
        FILE* jumbled = fopen("jumble.txt", "r");
    
    
        if ((dictionary == NULL) || (jumbled == NULL)){
            printf("Error, file does not exist.");
        }
    
    
        else{
    
    
            // Read in the number of words from the dictionary.
            int numwords;
            fscanf(dictionary, "%d", &numwords);
    
    
            int num_jumb;
            fscanf(jumbled, "%d", &num_jumb);
    
    
            // Go through each word.
            for (i = 0; i < numwords; i++) {
                fscanf(dictionary, "%s", list[i].word);
                strcpy(list[i].sort, list[i].word);
            }
    
    
                // Go through each jumbled word
                for (j = 0; j < num_jumb; j++) {
                    fscanf(jumbled, "%s", list[j].word);
                    strcpy(jumb[i].sort, jumb[i].word);
                }
    
    
            bubblesort(jumb[i].sort, strlen(list[i].sort));
    
    
            if (num_jumb < 100){
    
    
                // Print out the words and their other arrangement.
                for (l = 0; l < num_jumb; l++){
                        printf("Jumble Puzzle #%d: %s\n", l+1, list[l].word);
                }
                    for (k = 0; k < numwords; k++) {
                        if (strcmp(list[i].word, list[l].word) == 0) {
                            printf("%s\n", list[k].sort);
                        }
                    }
                }
                else{
                    printf("Your number must be less than 100.\n");
                }
            }
    
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're using local stack memory space for your word arrays, and there is precious little of it. Move your arrays above main, and let them stretch out in the heap memory (much larger).

    One dimension arrays of two word structs is just awkward, imo. Use two dimension arrays. For instance, you might think you have enough room currently for 30,000 words, but you don't - you have only 30,000 chars!

    Your first comment "//go through each word" is misleading, because you're just reading in and copying each word. I see no need to go through every jumbled word, after every word you read into the dictionary.

    You're trying to do everything in one or two loops, and that leads to errors. Use more loops, and do one or two things per loop. Then use another loop to do one or two more things.

    The speed up, if you want it (and it's huge), would be to read in all your words, then sort the words in both arrays, using the key of the sorted dictionary words. Now sort the jumble's you're looking for, and use a binary search. It'll knock your socks clean off!

    For your program however, you need to sort list[].sort, after every word you read in, and before you do anything with jumble. Jumble for loop should come after the read in and sorting of the dictionary words.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Your idea makes a little sense to me and I'm sure it would be much quicker and save a lot of memory, but there are a few things in the assignment that have to stay how they were posted in my original code. Like the bubble sort for example. I completed the code, even if it isn't the most logical or quickest way, but it does run correctly. Your help was much appreciated. For your viewing pleasure:




    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAX_WORD_LEN 6
    #define MAX_NUM_WORDS 30000
    
    
    // Struct to use.
    typedef struct {
        char word[MAX_WORD_LEN+1];
        char sort[MAX_WORD_LEN+1];
    } jumble_type;
    
    
    void bubblesort(char letters[], int length) {
    
    
        char temp;
        int i, j;
    
    
        // Sort each word alphabetically by letter.
        for(i = 0; i < length; i++){
            for(j = i; j < length; j++){
                if(letters[i] > letters[j]){
                    temp = letters[j];
                    letters[j] = letters[i];
                    letters[i] = temp;
    
    
                }
            }
        }
    }
    
    
    int main() {
    
    
        // Declare variables for dictionary and jumbled words.
        jumble_type list[MAX_NUM_WORDS];
        jumble_type jumb[MAX_NUM_WORDS];
    
    
        int i, j, k;
    
    
        // Open the files to write into.
        FILE* dictionary = fopen("jumbledct.txt", "r");
        FILE* jumbled = fopen("jumble.txt", "r");
    
    
        // Check to make sure files open.
        if ((dictionary == NULL) || (jumbled == NULL))
            printf("Error, file does not exist.");
    
    
        else {
            // Read in the number of words from the dictionary of words.
            int numwords;
            fscanf(dictionary, "%d", &numwords);
    
    
            // Read in the number of words from the jumble of words.
            int num_jumb;
            fscanf(jumbled, "%d", &num_jumb);
    
    
            // Go through each dictionary word, store in list, copy, then sort each.
            for (i = 0; i < numwords; i++) {
                fscanf(dictionary, "%s", list[i].word);
                strcpy(list[i].sort, list[i].word);
                bubblesort(list[i].sort, strlen(list[i].sort));
            }
    
    
                // Go through each jumbled word, store in jumb, copy, then sort each.
                for (i = 0; i < num_jumb; i++) {
                    fscanf(jumbled, "%s", jumb[i].word);
                    strcpy(jumb[i].sort, jumb[i].word);
                    bubblesort(jumb[i].sort, strlen(jumb[i].sort));
                }
    
    
            // Make sure number of jumbled words x: 0 < x < 100
            if ((num_jumb > 0) && (num_jumb < 100)) {
    
    
                // Print out the jumbled words.
                for (j = 0; j < num_jumb; j++){
                    printf("JUMBLE PUZZLE #%d: %s\n", j+1, jumb[j].word);
    
    
                    // Compare each word, then print out all dictionary word solutions.
                    int x=0;
                    for (k = 0; k < numwords; k++) {
                        if (strcmp(jumb[j].sort, list[k].sort) == 0) {
                            printf("%s\n", list[k].word);
                            x++;
                            printf("\n");
                        }
                    }
    
    
                    // Print out no solutions.
                    if (x == 0) {
                        printf("Sorry, this puzzle has no solutions.\n");
                        printf("\n");
                    }
                }
            }
    
    
            // Print out error for negative values or values over 100.
            else
                printf("Your number must be greater than 0 and less than 100.\n");
        }
    
    
        return 0;
    }

    Thank you for all your help. Newbie question: How do I mark as solved? Or did this post just do that?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your bubble sort algorithm, is not a bubble sort -- no "bubbles"! lol

    All bubble sorts work by comparing adjacent values. That is array[i] and array[i+1] indices are compared. That causes the "bubbling" effect to occur, during the sort.

    If you are comparing array[i] and array[j], then you are using a substitution sort - which is otherwise very similar to a bubble sort for all other parameters.

    Oddly, we don't have a solved flag for a thread.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Oops. Well substitution sort it is. Haha
    Thanks for letting me know.
    And thanks for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Jumble Program in C
    By deepdivin247 in forum C Programming
    Replies: 13
    Last Post: 04-22-2012, 09:39 PM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. jumble socket data
    By stormy in forum C Programming
    Replies: 10
    Last Post: 08-23-2005, 10:07 AM