Jumbling Strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sake
    New Member
    • Jan 2007
    • 46

    Jumbling Strings

    Hi,
    So now that I've figured out how to seed multiple random numbers, I'm faced with another problem. So basically I have a word (like "Manatee") that I want to jumble up, or mix each letter's position with another. So I have (7) random numbers each between zero and seven. So if "Manatee" is in string
    Code:
    char word1[7]="Manatee";
    and i have another string
    Code:
    char randWord1[7];
    I put characters from word1 using my random numbers as indexes in the second string, with a regularly incremented i. My only problem is that I sometimes have duplicates of my random characters, so i may turn out with: "eaeaMnte"( A true example).

    So using this code:

    Code:
    int n, i;
    char word1[7] = "Manatee";
    char randWord[7];
    for(i=0;i<=strlen(word1);i++){
          n = rand() / (RAND_MAX / strlen(word1) + 1);
          randWord[i] = word1[n];      
    }
    How could I make all my random numbers unique?
    -Austen
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I can think of 3 options open to you

    1. As you generate your random numbers check each number against all the previously generated numbers and discard any duplicates.

    2. Start with an array of numbers 0 - N repeatedly (say 100 * N times) generate 2 random numbers in the range 0 - N, swap the numbers at those indexes. Once finished you should have a fairly random list of numbers 0 - N inclusive

    3. Miss out the array of numbers, just repeatedly generate 2 random numbers in the range 0 - N and swap those letters in the word, you will never loose any letters

    Comment

    • sake
      New Member
      • Jan 2007
      • 46

      #3
      Thanks for all the help, your advice worked just fine.
      Here's the word jumble program that all this work has gone into. Sorry for the messy code, lack of functions, comments, and most any structured programming styles. This is more of just a "first draft":
      Code:
      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      
      #define WORDSIZE 10
      
      struct words{
             char word[20];
      };
      
      int main(){
          char *wordArray[WORDSIZE] = {"manatee", "walrus", "lobster", "jump", "harpoon", "opposite", "mother", "sunder", "sober", "unhappy", "illusion", "recoup"};
          struct words jumble[10];
          char singleWord[20];
          char guess[20];
          char temp;
          int seed;
          int whichWord;
          int i, n, n2;
          int guessAmount=0;
          int oldWhich;
          
          srand((unsigned)time(NULL));
          
          while(1==1){    
            guessAmount = 0;
            for(i=0;i<=WORDSIZE;i++){
              strcpy(jumble[i].word, wordArray[i]);
              //printf( "word: %s\n", jumble[i].word );
            }
        
            if(oldWhich>=0&&oldWhich<=WORDSIZE){
              //printf( "oldWhich: %d\n", oldWhich );
            }
            whichWord = rand() / (RAND_MAX / WORDSIZE + 1);
            if(whichWord==oldWhich){
              //printf( "nowinIF" );
              while(whichWord==oldWhich){
                whichWord = rand() / (RAND_MAX / WORDSIZE + 1);
              }
            }
            oldWhich = whichWord;
            //printf( "oldWhich2: %d\n", oldWhich );
            strcpy( singleWord, jumble[whichWord].word );
            for(i=0;i<=strlen(singleWord);i++){
              n = rand() / (RAND_MAX / strlen(singleWord) + 1);
              n2 = rand() / (RAND_MAX / strlen(singleWord) + 1);
              temp = singleWord[n];
              singleWord[n] = singleWord[n2];
              singleWord[n2] = temp;
              //printf( "n: %d\n", n );
              //randWord[i] = jumble[whichWord].word[n];      
            }
          
            printf( "%s\n", singleWord );
          
            while(strcmp(guess, jumble[whichWord].word ) != 0){
              scanf( "%s", &guess );
              if( strcmp( guess, jumble[whichWord].word ) != 0){
                  printf( "You are incorrect\n" );
              }
              guessAmount++;
            }
          
            printf( "You are correct with %d guesses!\n", guessAmount );
            system( "pause" );
          }   
      }
      Tested on Dev-C++ (gcc) with Windows XP...

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        This

        scanf( "%s", &guess );

        is very dangerous because there is nothing to stop me putting in more data than the size of guess. However this

        fgets(guess, sizeof guess, stdin);

        does exactly the same thing but guarantees not to write data passed the end of guess.

        I can see 2 problems with your program design.

        1. For someone that absolutely can not guess the word there is no way to exit
        2. If you used a word that is an anagram of another word 'made' for instance and the user guesses the other word the program doesn't accept it as an answer.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Originally posted by sake
          Thanks for all the help, your advice worked just fine.
          Here's the word jumble program that all this work has gone into. Sorry for the messy code, lack of functions, comments, and most any structured programming styles. This is more of just a "first draft":
          Code:
          #include <stdio.h>
          #include <stdlib.h>
          #include <time.h>
          
          #define WORDSIZE 10
          
          struct words{
                 char word[20];
          };
          
          int main(){
              char *wordArray[WORDSIZE] = {"manatee", "walrus", "lobster", "jump", "harpoon", "opposite", "mother", "sunder", "sober", "unhappy", "illusion", "recoup"};
              struct words jumble[10]; // Why not get rid of wordArray and set your words structs to the words above?  Or simply use wordArray and not the words struct?
              char singleWord[20];
              char guess[20];
              char temp;
              int seed; // Where is seed used?
              int whichWord;
              int i, n, n2;
              int guessAmount=0;
              int oldWhich;
              
              srand((unsigned)time(NULL));
              
              while(1==1){ // THis can be while (1) or while (true) - the same effect, but less 'messy'
                guessAmount = 0;
                for(i=0;i<=WORDSIZE;i++){
                  strcpy(jumble[i].word, wordArray[i]);
                  //printf( "word: %s\n", jumble[i].word );
                }
            
                if(oldWhich>=0&&oldWhich<=WORDSIZE){ // This if statement has no inside, so consider removing it
                  //printf( "oldWhich: %d\n", oldWhich );
                }
                whichWord = rand() / (RAND_MAX / WORDSIZE + 1);
                if(whichWord==oldWhich){ // This if statement is unnecessary.  You can have the
                  //printf( "nowinIF" ); // while loop stand alone, since it will only execute if whichWord == oldWhich
                  while(whichWord==oldWhich){
                    whichWord = rand() / (RAND_MAX / WORDSIZE + 1);
                  }
                }
                oldWhich = whichWord;
                //printf( "oldWhich2: %d\n", oldWhich );
                strcpy( singleWord, jumble[whichWord].word );
                for(i=0;i<=strlen(singleWord);i++){
                  n = rand() / (RAND_MAX / strlen(singleWord) + 1);
                  n2 = rand() / (RAND_MAX / strlen(singleWord) + 1);
                  temp = singleWord[n];
                  singleWord[n] = singleWord[n2];
                  singleWord[n2] = temp;
                  //printf( "n: %d\n", n );
                  //randWord[i] = jumble[whichWord].word[n];      
                }
              
                printf( "%s\n", singleWord );
              
                while(strcmp(guess, jumble[whichWord].word ) != 0){ // You could prompt the user for input here a.k.a.
                  // printf("Enter your guess: ");
                  // You may want to re-display the jumbled word, too.
                  scanf( "%s", &guess );
                  if( strcmp( guess, jumble[whichWord].word ) != 0){
                      printf( "You are incorrect\n" );
                  }
                  guessAmount++;
                }
              
                printf( "You are correct with %d guesses!\n", guessAmount );
                system( "pause" );
              }   
          }
          Tested on Dev-C++ (gcc) with Windows XP...
          There is actually no exit option currently - perhaps you could add the option of quitting at every guess point, and after a correct guess. An easy way to do this would be to have a control variable (type char, name continue) initialized to 'y' - then set your while loop condition to

          Code:
          while (continue == 'y' || continue == 'Y')
          to continue executing while the user hits y for continue playing.

          During your guess loop, ask the user for their guess or to press q for quit - if they wish to quit, set continue to 'n' and break from the guessing loop. Also prompt the user for quitting after you display their congratulations message.

          Finally, there are just a few places where your code can be cleaned up. I have added some comments around these areas.

          Comment

          Working...