-2

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.

  • Please choose a title which would be useful for others with the same issue. Currently, it contains no information about your issue. – user438383 Jan 05 '22 at 19:17
  • 1
    "what this rand() % 4 means" It will pick a pseudo random number in [0, 3]. – Bob__ Jan 05 '22 at 19:23
  • If you don't need letters, then just ignore letters and goto the start. It's kind of inefficient. See [Duff's device](https://stackoverflow.com/q/514118/2472827) if you want to see code that serves a purpose. – Neil Jan 05 '22 at 19:27
  • what can we use instead of `goto` – Anirudh Chowdary Jan 05 '22 at 19:52
  • @AnirudhChowdary Since you are already using functions, seems natural to use an array of function pointers. Instead of `inc`, just have them add functions based on your selection directly. Make sure it has at least one. – Neil Jan 05 '22 at 21:09

2 Answers2

2
  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.

The following takes care of the goto, but not the other concerns.

for (int i = 0; i < length; i++)
{
    bool retry = true;
    while (retry) }
        switch (rand() % 4) {
          case 0:
              if (inc[0]) { password[i] = upperCase(); retry = false; }
              break;
          case 1:
              if (inc[0]) { password[i] = lowerCase(); retry = false; }
              break;
          case 2:
              if (inc[1]) { password[i] = number(); retry = false; }
              break;
          case 3:
              if (inc[2]) { password[i] = symbol(); retry = false; }
              break;
          }
    }
}
Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81
0

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.

Oka
  • 23,367
  • 6
  • 42
  • 53
Asad
  • 279
  • 1
  • 7