Thread: Another Jumble Program in C

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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