Scramble a String – Solution

For this month’s Exercise, my solution to the scramble() function requires a second array. You could code a scramble() function without using a second array (or storage buffer), swapping individual characters within the string, but I chose not to.

The scramble() function’s first task is to create a char buffer large enough to store the incoming string. Further, I want the buffer to be filled with the null character, \0. The calloc() function handles this task, using variable key as the char pointer to hold the allocated buffer’s address and len as the string size, plus one for the terminating null character:

key = calloc( len+1, sizeof(char) );

Upon success, my solution uses the key buffer to store the scrambled string. Here is my complete scramble() function:

void scramble(char *p,int len)
{
    char *key;
    int x,r;

    /* allocate the temporary string and
       fill will null characters */
    key = calloc( len+1, sizeof(char) );
    if( key==NULL )
    {
        fprintf(stderr,"Memory allocation error\n");
        exit(1);
    }

    /* process the string */
    x = 0;
    while(x<len)
    {
        r = rand() % len;        /* generate random value */
        if( !*(key+r) )          /* look for a null char */
        {
            *(key+r) = *(p+x);   /* assign the character */
            x++;                 /* increment the offset */
        }
    }

    /* update the scrambled string */
    strcpy( p, key);
}

Variable x acts as an index into the passed string p. The while loop spins until all characters in p are processed.

First, a random value r is generated between 0 and the string’s length, len.

Next, a test is done to determine whether a null character exists at position r in buffer key: if( !*(key+r) ) This expression is true when the character is null. Otherwise, the while loop keeps spinning with additional random values generated.

When a null character is found in key, it’s replaced by the sequential character in p, referenced by variable x:

*(key+r) = *(p+x);

Variable x is then incremented, and the while loop continues.

Click here to view my full solution, which includes the header files stdio.h, stdlib.h, string.h, and time.h. Each of these are required for functions used in the main() skeleton presented in the original post, so none of them should surprise you.

This solution reminds me of the Lotto code in my C programming books. It works on a similar basis: using an array to track random items. Though the “array” in my scramble() function is an allocated buffer, the concept is the same.

I hope your solution meets with success. Especially if you used the character swap method, which I would find a fascinating approach to the problem.

2 thoughts on “Scramble a String – Solution

  1. Thanks for sorting out the mess I made of formatting my solution. I realised afterwards I could have declared c1 and c2 outside the while loop.

    Of course if you were writing an anagram game you’d need to keep the original for checking answers so would need to pass a copy to the function to be scrambled.

  2. Yes, it’s easier if the function returns the string, which is why I set up the Exercise the way I did.

Leave a Reply