Thread: problem with array

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    29

    problem with array

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define BUFFER 20
    
    char input_Command[BUFFER], jumbled_Data[BUFFER];
    
    int main()
    {
    	int i, num;
    
    	// Set the random source
    	srand( (unsigned)time( NULL ) );
    
    	for (i=0; i < 4; i++) {
    	jumbled_Data[i] = rand()%26+'a';
    	}
    
    	printf("command>");
    	gets(input_Command); 
    	num = strlen(input_Command); // get length of command
    
    	for (i=0; i < num; i++) {
    	strcat(&input_Command[i], jumbled_Data); 
    	}
    
    	printf("%s\n", &input_Command[0]);
    
    return(0);
    }
    Output:
    user@titan:~/code/c$ ./jumble
    command>crap
    crapcvtccvtccvtccvtc


    But I *need* the output to be:
    ccvtcrcvtcacvtcpcvtc

    I realize this is most likely a problem with my lack of knowledge concerning arrays and basically C in general, but I am learning.
    -thanks in advance for any help

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is something similar, done a little differently.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    int main()
    {
       char command[20], jumbled[sizeof command * 4];
       size_t i, j, k;
       /*
        * Set the random source.
        */
       srand( (unsigned)time( NULL ) );
       /*
        * Prompt user.
        */
       fputs("command>", stdout);
       fflush(stdout);
       /*
        * Obtain user input.
        */
       if ( fgets(command, sizeof command, stdin) )
       {
          char *newline = strchr(command, '\n');
          if ( newline )
          {
             *newline = '\0'; /* strip trailing '\n' */
          }
       }
       /*
        * Jumble things up.
        */
       for ( i = 0, k = 0; command[i] != '\0'; ++i )
       {
          jumbled[k++] = command[i]; /* add "real" data */
          /*
           * Insert 4 random characters.
           */
          for ( j = 0; j < 4; ++j )
          {
             jumbled[k++] = rand() % 26 + 'a';
          }
       }
       /*
        * Display the result.
        */
       puts(jumbled);
    
       return 0;
    }
    
    /* my output
    command>crap
    cbrkmrkoxtabjorpxjik
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    29
    ahhhhg thank you!

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    29
    Actually my otuput is rather strange.
    command>bleh
    blutllidwneriqghueexôõÿ¿…@
    f@œöÿ¿€M@b

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    29
    Nevermind I just used your for loop with rand() in it in my script now it's working perfectly. I do thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM