Home > Back-end >  What can be used instead of `goto` in this code?
What can be used instead of `goto` in this code?

Time:01-06

What does the line else goto PICK do? and what does rand() % 4 do?

Full code snippet:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char lowerCase () {
    char lc[] = "abcdefghijklmnopqrstuvwxyz";
    return lc[rand() % 26];
}

char upperCase () {
    char uc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    return uc[rand() % 26];
}

char number () {
    char num[] = "1234567890";
    return num[rand() % 10];
}

char symbol () {
    char sym[] = "@':;.,/?<>#~[]{}";
    return sym[rand() % 16];
}

int main () {
    int length = 37;
    int inc[] = {1, 1, 0};
    srand(time(NULL)); // Generate seed


    printf("Welcome to Password Generator \n");

    printf("Length of the password: ");
    //while (!length && length != 0)
    scanf("%i", &length);

    char password[length];

    printf("Do you need Letters in your password ? if yes type '1', if no type '0':");
    scanf("%i", &inc[0]);
    printf("Do you need numbers in your password ? if yes type '1', if no type '0':");
    scanf("%i", &inc[1]);
    printf("Do you need symbols in your password ? if yes type '1', if no type '0':");
    scanf("%i", &inc[2]);

    for (int i = 0; i < length; i  ) {
PICK: switch (rand() % 4) { // Random number 0 - 1
          case 0:
              if (inc[0]) password[i] = upperCase();
              else goto PICK;
              break;
          case 1:
              if (inc[0]) password[i] = lowerCase();
              else goto PICK;
              break;
          case 2:
              if (inc[1]) password[i] = number();
              else goto PICK;
              break;
          case 3:
              if (inc[2]) password[i] = symbol();
              else goto PICK;
      }
    }

    printf("Your Password: %s\n", password);
}

I did not understand what the goto statement does and also what is PICK.and why did they declare an array specifically to be {1,1,0}.and actually I didn't understand anything in this code .if anyone could summarize about the functions used in this code it would be better.

CodePudding user response:

  1. The switch statement is generating a random number between 0 and 3. This picks a 'random' switch case.

  2. For each case, it checks to see if the user selected that option. If they didn't, it jumps back to the top of the switch and tries again.

The default for the include options is to use letters and numbers, but not symbols.

There are numerous problems with this code snippet.

  • goto is generally to be avoided as a flow-control mechanism.

  • There's no guarantee that it will terminate.

  • The helper functions should use static const char arrays so it doesn't build them on the stack every time they are called.

  • It's also cryptographically questionable since rand is known to have numerous problems when used for security contexts.

CodePudding user response:

switch (rand() % 4) { // Random number 0 - 1

(rand % 4) despite the comment, returns a number between 0 and 3

PICK: is a label. What the code does is jump to PICK when the conditions if (inc[0]) and if (inc[1]) are not met in the cases. The code can also be written as:

for (int i = 0; i < length; i  ) {
    switch (rand() % 4) { // Random number 0 - 3
        case 0:
            if (inc[0]) password[i] = upperCase();
            else i--;
            break;
        case 1:
            if (inc[0]) password[i] = lowerCase();
            else i--;
            break;
        case 2:
            if (inc[1]) password[i] = number();
            else i--;
            break;
        case 3:
            if (inc[2]) password[i] = symbol();
            else i--;
            break; // Do not ommit the break
    }
}

CodePudding user response:

rand()

First let's look at what `rand()` is and what `rand() % 4` does:

int rand(void) or simply rand() function generates a random number, the points to note are:

  1. Random number is in the range from 0 to RAND_MAX.
  2. RAND_MAX is a constant defined in <stdlib.h> and is guaranteed to be at least 32767.

So what % 4 does is it divides the generated random number by 4 and returns (outputs) the remainder.

Refer here to understand what % operator is.


goto

The goto statement marks a jump statement, it simply means moving the execution to the predefined label.

In your code: PICK is the label and whenever you have a goto statement the execution jumps to that label.

  •  Tags:  
  • Related