Answer to Question #121013 in C for Mst. Bithy

Question #121013
Cyber Security is a key issue to protect our daily documents and applications stored
and submitted in various platforms. Having a robust encryption system to our
generated password is very essential in this perspective. Your task is to create a nice
and smooth encrypted password generator. Follow the instructions carefully to build
the password generator.

a. Take a 5 digit integer number from user. If the number is not exactly
of 5 digits, give a warning message to user and exit/abort the program.
b. In case of a 5 digit number, convert the number to a corresponding
character.
c. Finally, generate a 5 length password and show it on screen.
1
Expert's answer
2020-06-11T07:40:29-0400
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    int number;
    printf("Enter a 5-digit number: " );
    if (scanf("%d", &number) != 1 || number < 10000 || number > 99999) {
        printf("Invalid input\n");
        return 1;
    }

    char symbols[5];
    printf("Digits (in reverse order):");
    for (int i = 0; i < 5; i++) {
        symbols[i] = '0' + number % 10;
        number /= 10;
        printf(" %c", symbols[i]);
    }
    printf("\n");

    /* https://en.wikipedia.org/wiki/Fisher#Yates_shuffle */
    srand((unsigned) time(NULL));
    char swap;
    for (int i = 4; i > 0; i--) {
        int j = rand() % (i + 1);
        swap = symbols[i], symbols[i] = symbols[j], symbols[j] = swap;
    }

    printf("Random permutation: ");
    for (int i = 0; i < 5; i++)
        printf("%c", symbols[i]);
    printf("\n");

    printf("Password: ");
    for (int i = 0; i < 5; i++)
        printf("%c", symbols[i] - '0' + (rand() % 2 == 0 ? 'a' : 'A'));
    for (int i = 4; i >= 0; i--)
        printf("%c", (rand() % 2 == 0 ? 'Z' : 'z') - (symbols[i] - '0'));
    printf("\n");

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS