Skip to main content

Take this line from every branch and put it at the end of the loop:

printf("%c", password[i]);

Take this from every branch and put it at the start of the loop (and delete it from before the loop):

randomizer = rand() % 4;

As Schwern observes, you can also turn the if chain into a switch/case, which is a bit faster and easier to read.

This makes the loop look like:

for (i=0;i<n;i++)
{
    randomizer = rand() % 4;
    ifswitch (randomizer == 1) {
    {case 1:
        password[i] = numbers[rand() % 10];
    }
    else if (randomizer == 2)break;
    {case 2:
        password[i] = symbols[rand() % 26];
    }
    else if (randomizer == 3)break;
    {case 3:
        password[i] = letterr[rand() % 26];
    }
    elsebreak;
    {default:
        password[i] = letter[rand() % 21];
        break;
    }
    printf("%c", password[i]);
}

Take this line from every branch and put it at the end of the loop:

printf("%c", password[i]);

Take this from every branch and put it at the start of the loop (and delete it from before the loop):

randomizer = rand() % 4;

This makes the loop look like:

for (i=0;i<n;i++)
{
    randomizer = rand() % 4;
    if(randomizer == 1)
    {
        password[i] = numbers[rand() % 10];
    }
    else if (randomizer == 2)
    {
        password[i] = symbols[rand() % 26];
    }
    else if (randomizer == 3)
    {
        password[i] = letterr[rand() % 26];
    }
    else
    {
        password[i] = letter[rand() % 21];
    }
    printf("%c", password[i]);
}

Take this line from every branch and put it at the end of the loop:

printf("%c", password[i]);

Take this from every branch and put it at the start of the loop (and delete it from before the loop):

randomizer = rand() % 4;

As Schwern observes, you can also turn the if chain into a switch/case, which is a bit faster and easier to read.

This makes the loop look like:

for (i=0;i<n;i++)
{
    randomizer = rand() % 4;
    switch (randomizer) {
    case 1:
        password[i] = numbers[rand() % 10];
        break;
    case 2:
        password[i] = symbols[rand() % 26];
        break;
    case 3:
        password[i] = letterr[rand() % 26];
        break;
    default:
        password[i] = letter[rand() % 21];
        break;
    }
    printf("%c", password[i]);
}
clarified language to make answer easier to understand
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

Take itthis line from everywhere toevery branch and put it at the end of the cycleloop:

 printf("%c", password[i]);
printf("%c", password[i]);

Take itthis from everywhere toevery branch and put it at the beginstart of the cycleloop (and delete it from begin ofbefore the programloop):

randomizer = rand() % 4;

This makes the loop look like:

for (i=0;i<n;i++)
{
    randomizer = rand() % 4;
    if(randomizer == 1)
    {
        password[i] = numbers[rand() % 10];
    }
    else if (randomizer == 2)
    {
        password[i] = symbols[rand() % 26];
    }
    else if (randomizer == 3)
    {
        password[i] = letterr[rand() % 26];
    }
    else
    {
        password[i] = letter[rand() % 21];
    }
    printf("%c", password[i]);
}

Take it from everywhere to the end of the cycle:

 printf("%c", password[i]);

Take it from everywhere to the begin of the cycle(and delete it from begin of the program):

randomizer = rand() % 4;

Take this line from every branch and put it at the end of the loop:

printf("%c", password[i]);

Take this from every branch and put it at the start of the loop (and delete it from before the loop):

randomizer = rand() % 4;

This makes the loop look like:

for (i=0;i<n;i++)
{
    randomizer = rand() % 4;
    if(randomizer == 1)
    {
        password[i] = numbers[rand() % 10];
    }
    else if (randomizer == 2)
    {
        password[i] = symbols[rand() % 26];
    }
    else if (randomizer == 3)
    {
        password[i] = letterr[rand() % 26];
    }
    else
    {
        password[i] = letter[rand() % 21];
    }
    printf("%c", password[i]);
}
Source Link
Miron
  • 418
  • 2
  • 15

Take it from everywhere to the end of the cycle:

 printf("%c", password[i]);

Take it from everywhere to the begin of the cycle(and delete it from begin of the program):

randomizer = rand() % 4;