Untitled

 avatar
unknown
c_cpp
a year ago
982 B
5
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to generate a random password
void generatePassword(char password[], int length) {
    const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-=_+[]{}|;:'\",.<>/?";
    const int charsetLength = sizeof(charset) - 1;

    srand((unsigned int)time(NULL));

    for (int i = 0; i < length; ++i) {
        int index = rand() % charsetLength;
        password[i] = charset[index];
    }

    password[length] = '\0'; // Null-terminate the string
}

int main() {
    int length;

    printf("Enter the length of the password: ");
    scanf("%d", &length);

    if (length <= 0) {
        printf("Invalid password length. Please enter a positive integer.\n");
        return 1; // Exit with an error code
    }

    char password[length + 1]; // +1 for null terminator

    generatePassword(password, length);

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

    return 0;
}
Editor is loading...
Leave a Comment