473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Jumbling Strings

46
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
Expand|Select|Wrap|Line Numbers
  1. char word1[7]="Manatee";
and i have another string
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. int n, i;
  2. char word1[7] = "Manatee";
  3. char randWord[7];
  4. for(i=0;i<=strlen(word1);i++){
  5.       n = rand() / (RAND_MAX / strlen(word1) + 1);
  6.       randWord[i] = word1[n];      
  7. }
  8.  
How could I make all my random numbers unique?
-Austen
Jan 9 '07 #1
4 4000
Banfa
9,065 Expert Mod 8TB
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
Jan 9 '07 #2
sake
46
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":
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define WORDSIZE 10
  6.  
  7. struct words{
  8.        char word[20];
  9. };
  10.  
  11. int main(){
  12.     char *wordArray[WORDSIZE] = {"manatee", "walrus", "lobster", "jump", "harpoon", "opposite", "mother", "sunder", "sober", "unhappy", "illusion", "recoup"};
  13.     struct words jumble[10];
  14.     char singleWord[20];
  15.     char guess[20];
  16.     char temp;
  17.     int seed;
  18.     int whichWord;
  19.     int i, n, n2;
  20.     int guessAmount=0;
  21.     int oldWhich;
  22.  
  23.     srand((unsigned)time(NULL));
  24.  
  25.     while(1==1){    
  26.       guessAmount = 0;
  27.       for(i=0;i<=WORDSIZE;i++){
  28.         strcpy(jumble[i].word, wordArray[i]);
  29.         //printf( "word: %s\n", jumble[i].word );
  30.       }
  31.  
  32.       if(oldWhich>=0&&oldWhich<=WORDSIZE){
  33.         //printf( "oldWhich: %d\n", oldWhich );
  34.       }
  35.       whichWord = rand() / (RAND_MAX / WORDSIZE + 1);
  36.       if(whichWord==oldWhich){
  37.         //printf( "nowinIF" );
  38.         while(whichWord==oldWhich){
  39.           whichWord = rand() / (RAND_MAX / WORDSIZE + 1);
  40.         }
  41.       }
  42.       oldWhich = whichWord;
  43.       //printf( "oldWhich2: %d\n", oldWhich );
  44.       strcpy( singleWord, jumble[whichWord].word );
  45.       for(i=0;i<=strlen(singleWord);i++){
  46.         n = rand() / (RAND_MAX / strlen(singleWord) + 1);
  47.         n2 = rand() / (RAND_MAX / strlen(singleWord) + 1);
  48.         temp = singleWord[n];
  49.         singleWord[n] = singleWord[n2];
  50.         singleWord[n2] = temp;
  51.         //printf( "n: %d\n", n );
  52.         //randWord[i] = jumble[whichWord].word[n];      
  53.       }
  54.  
  55.       printf( "%s\n", singleWord );
  56.  
  57.       while(strcmp(guess, jumble[whichWord].word ) != 0){
  58.         scanf( "%s", &guess );
  59.         if( strcmp( guess, jumble[whichWord].word ) != 0){
  60.             printf( "You are incorrect\n" );
  61.         }
  62.         guessAmount++;
  63.       }
  64.  
  65.       printf( "You are correct with %d guesses!\n", guessAmount );
  66.       system( "pause" );
  67.     }   
  68. }
  69.  
  70.  
Tested on Dev-C++ (gcc) with Windows XP...
Jan 9 '07 #3
Banfa
9,065 Expert Mod 8TB
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.
Jan 9 '07 #4
Ganon11
3,652 Expert 2GB
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":
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define WORDSIZE 10
  6.  
  7. struct words{
  8.        char word[20];
  9. };
  10.  
  11. int main(){
  12.     char *wordArray[WORDSIZE] = {"manatee", "walrus", "lobster", "jump", "harpoon", "opposite", "mother", "sunder", "sober", "unhappy", "illusion", "recoup"};
  13.     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?
  14.     char singleWord[20];
  15.     char guess[20];
  16.     char temp;
  17.     int seed; // Where is seed used?
  18.     int whichWord;
  19.     int i, n, n2;
  20.     int guessAmount=0;
  21.     int oldWhich;
  22.  
  23.     srand((unsigned)time(NULL));
  24.  
  25.     while(1==1){ // THis can be while (1) or while (true) - the same effect, but less 'messy'
  26.       guessAmount = 0;
  27.       for(i=0;i<=WORDSIZE;i++){
  28.         strcpy(jumble[i].word, wordArray[i]);
  29.         //printf( "word: %s\n", jumble[i].word );
  30.       }
  31.  
  32.       if(oldWhich>=0&&oldWhich<=WORDSIZE){ // This if statement has no inside, so consider removing it
  33.         //printf( "oldWhich: %d\n", oldWhich );
  34.       }
  35.       whichWord = rand() / (RAND_MAX / WORDSIZE + 1);
  36.       if(whichWord==oldWhich){ // This if statement is unnecessary.  You can have the
  37.         //printf( "nowinIF" ); // while loop stand alone, since it will only execute if whichWord == oldWhich
  38.         while(whichWord==oldWhich){
  39.           whichWord = rand() / (RAND_MAX / WORDSIZE + 1);
  40.         }
  41.       }
  42.       oldWhich = whichWord;
  43.       //printf( "oldWhich2: %d\n", oldWhich );
  44.       strcpy( singleWord, jumble[whichWord].word );
  45.       for(i=0;i<=strlen(singleWord);i++){
  46.         n = rand() / (RAND_MAX / strlen(singleWord) + 1);
  47.         n2 = rand() / (RAND_MAX / strlen(singleWord) + 1);
  48.         temp = singleWord[n];
  49.         singleWord[n] = singleWord[n2];
  50.         singleWord[n2] = temp;
  51.         //printf( "n: %d\n", n );
  52.         //randWord[i] = jumble[whichWord].word[n];      
  53.       }
  54.  
  55.       printf( "%s\n", singleWord );
  56.  
  57.       while(strcmp(guess, jumble[whichWord].word ) != 0){ // You could prompt the user for input here a.k.a.
  58.         // printf("Enter your guess: ");
  59.         // You may want to re-display the jumbled word, too.
  60.         scanf( "%s", &guess );
  61.         if( strcmp( guess, jumble[whichWord].word ) != 0){
  62.             printf( "You are incorrect\n" );
  63.         }
  64.         guessAmount++;
  65.       }
  66.  
  67.       printf( "You are correct with %d guesses!\n", guessAmount );
  68.       system( "pause" );
  69.     }   
  70. }
  71.  
  72.  
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

Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
2
by: Wade | last post by:
how to fix this? Date Raceway DO Temp Mort 01/01/04 1 10 15 0 01/02/04 1 10 15 0 01/02/04 2 10 15 1 01/01/04 2 10 15 4 01/01/04 ...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
14
by: manstey | last post by:
Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.