randomizing strings

This is a discussion on randomizing strings within the C Programming forums, part of the General Programming Boards category; hi lets assume i want to get input from a user: Code: ....... char buffer[256]; gets(buffer); ....... the user enters ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    randomizing strings

    hi lets assume i want to get input from a user:
    Code:
    .......
    char buffer[256];
    gets(buffer);
    .......
    the user enters some text like "abc".
    is there a function that would randomize that string?
    e.g. out of "abc": "cba", "cab", "bca", "acb".

    thanks in advance threadhead

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Here's a site about shuffling a deck of cards. The theory is also applicable to shuffling characters in a string.

    http://c2.com/cgi/wiki?LinearShuffle

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks for your reply but i just dont get main idea of
    this randomizing thing.

    can you maybe give me an example?

    cu

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i thought of a random algorythm.
    heres my result:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main ()
    {
       int i;
       char input[20];
       srand(time(NULL));
       scanf("%s", input);
    
       for(i=0;i<20;i++)
          input[i] = input[rand () % 20];
    
       printf("%s", input);
       return 0;
    }
    but when i enter i.e. "123" i get alot of strange symbols, but the result should be containing only
    the entered chars.

    anyone?
    thanks

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    with this code i swap the first with the second letter
    of the buffer.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //#include <time.h>
    
    int main ()
    {
        
    //    char remember[1024];
        char buffer[] = "ha";
        char tmp[1024];
        int i;
        int le;
    //    srand(time(NULL);
        le = (strlen(buffer));
        
        tmp[le-1] = buffer[le-2];
        tmp[le-2] = buffer[le-1];
        
        strncpy(buffer, tmp, strlen(buffer));
    
        printf("%s\n", buffer);
    
    
        
        return 0;
    }
    but the more characters the buffer has the more complicated
    it would get to copy the single chars.
    is it possible to manage this with a loop?
    if yes so please give me an example, im stuck

    thank you

  6. #6
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    I was thinking that this was easy but .... ive changed my mind
    heres what i got so far (after 5 mins) the only prob is that some letters arent there anylonger... u'l c
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    int main(){
    
    	char woord[5]="HALLO",letter;
    	int lengte,i,ct;
    
    	time_t t;
    	lengte=strlen(woord);
    	srand((unsigned) time(&t));
    	printf("%s\n",woord);
    
    	for(i=0;i<lengte;i++){
    								 ct=rand()%lengte;
    								 letter=woord[ct];
    								 printf("%c",letter);
    								 }
    
    	return 0;
    	}
    then u get as output something like
    LAALH or OHHLO
    so this way not all letters are being used in the mixed strings....
    (and right now im going to sleep so ull have to figure the rest out urself ..)
    ::::edit::::
    again sry bout the indentations but it seems that if i ctrl-c ctrl-v the whole thing it gets messed up

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need a swap function (or at least a working section of code to swap 2 variables)
    Code:
    #include <stdio.h>
    
    void swap(char *a, char *b);
    
    int main(void)
    {
        char    name[] = "Hammer";
    
        puts(name);
    
        swap(&name[1], &name[4]);
    
        puts(name);
    
        return(0);
    }
    
    void swap(char *a, char *b)
    {
        char    c;
        c = *a;
        *a = *b;
        *b = c;
    }
    Now, adapt this code to fit your needs
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    another questions was popping up in mind when i
    was adding your code to mine.

    lets assume a user enter a text like "abc".
    now i want to implement a code that tries out
    every possible combination with that string.
    like : "abc", "acb", "bac", "bca", "cba", cab"

    how would i do that?
    thanks

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    IIRC, Prelude wrote some code that'll do that, and again, IIRC, it's somewhere on the C++ board. Happy hunting
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21