Skip to content

Instantly share code, notes, and snippets.

@coresh
Forked from sahil-patel6/passwordGenerator.c
Created June 8, 2025 16:57
Show Gist options
  • Save coresh/1ed9ca50433ce7b885780ae946264a4e to your computer and use it in GitHub Desktop.
Save coresh/1ed9ca50433ce7b885780ae946264a4e to your computer and use it in GitHub Desktop.

Revisions

  1. @sahil-patel6 sahil-patel6 revised this gist Jul 1, 2018. No changes.
  2. @sahil-patel6 sahil-patel6 revised this gist Jan 5, 2018. 1 changed file with 19 additions and 1 deletion.
    20 changes: 19 additions & 1 deletion passwordGenerator.c
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,25 @@ int main() {

    //calling printPassword function to display the password selected by the user from the above options

    printPassword();
    printf("How many passwords do you want??\n");

    char passwordTimes[1];

    gets(passwordTimes);

    int m = atoi(passwordTimes);

    printf("%d",m);

    while (m>0){

    printPassword();

    m--;

    _sleep(1000);

    }

    char isAgain[1];

  3. @sahil-patel6 sahil-patel6 revised this gist Jan 5, 2018. 1 changed file with 26 additions and 2 deletions.
    28 changes: 26 additions & 2 deletions passwordGenerator.c
    Original file line number Diff line number Diff line change
    @@ -13,9 +13,13 @@ char passwordLength[];
    void printPassword();

    int main() {


    //using srand with time to update random function to generate random numbers with time

    srand(time(NULL));

    //A brief introduction

    printf("Welcome To Password Generator 2018\n");

    printf("You can generate any kind of password you would like to...\n");
    @@ -28,18 +32,26 @@ int main() {

    printf("Enter any type given above(recommended == e)\n");

    //getting user input on what type of password the need..

    gets(passwordType);

    printf("Enter the password length\n");

    //prompting user for password length

    gets(passwordLength);

    //calling printPassword function to display the password selected by the user from the above options

    printPassword();

    char isAgain[1];

    printf("Do you want to generate password again(y/n)\n");

    //asking user if he/she wants to generate password again, if yes then calling main function again

    gets(isAgain);

    if (isAgain[0] == 'y'){
    @@ -61,12 +73,16 @@ int main() {

    }

    // This function generating random numbers to generate password

    int generateRandomNumbers(){

    int random;

    int r;

    // based on passwordType the user needs, generating random numbers

    switch (passwordType[0]){

    case 'a':
    @@ -321,14 +337,20 @@ int generateRandomNumbers(){

    }

    // Here is the main function which prints the password to the user

    void printPassword(){

    int randomNumber;

    int k;

    //converting password length string to ineger to loop

    int length = atoi(passwordLength);

    //looping and generating random numbers and storing in password array

    for (k = 0; k < length; k++) {

    randomNumber = generateRandomNumbers();
    @@ -345,6 +367,8 @@ void printPassword(){

    printf("\n");

    //Here Printing the password to the user

    printf("Your password is: %s", password);

    printf("\n");
    @@ -357,4 +381,4 @@ void printPassword(){

    printf("\n");

    }
    }
  4. @sahil-patel6 sahil-patel6 revised this gist Dec 31, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion passwordGenerator.c
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    #include <time.h>
    #include <stdlib.h>

    char passwordType[5];
    char passwordType[1];

    int generateRandomNumbers();

  5. @sahil-patel6 sahil-patel6 renamed this gist Dec 31, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @sahil-patel6 sahil-patel6 created this gist Dec 31, 2017.
    360 changes: 360 additions & 0 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,360 @@
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>

    char passwordType[5];

    int generateRandomNumbers();

    char password[512];

    char passwordLength[];

    void printPassword();

    int main() {

    srand(time(NULL));

    printf("Welcome To Password Generator 2018\n");

    printf("You can generate any kind of password you would like to...\n");

    printf("Choose one of type below , you can choose both or triple too\n");

    printf("a) a to z \nb) A to Z \nc) 0 to 9 \nd) special characters only\ne) anything\nf) a & b\ng) a & c\n"
    "h) a & d \ni) b & c \nj) b & d\nk) c & d\nl) a, b & c\nm) b, c & d\nn) a,c, &d "
    "\no)a,b &d\np)a,b,c &d\n");

    printf("Enter any type given above(recommended == e)\n");

    gets(passwordType);

    printf("Enter the password length\n");

    gets(passwordLength);

    printPassword();

    char isAgain[1];

    printf("Do you want to generate password again(y/n)\n");

    gets(isAgain);

    if (isAgain[0] == 'y'){

    main();

    } else if (isAgain[0] == 'n'){

    printf("Thanks for using my password generator\n");

    } else{

    printf("Invalid input\n");

    }


    return 0;

    }

    int generateRandomNumbers(){

    int random;

    int r;

    switch (passwordType[0]){

    case 'a':

    random = (rand() % (122 + 1 - 97)) + 97;

    break;

    case 'b':

    random = (rand() % (90 + 1 - 65)) + 65;

    break;

    case 'c':

    random = (rand() % (57 + 1 - 48)) + 48;

    break;

    case 'd':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if (((r<=47) && (r>=33)) || ((r<=64) && (r>=58)) || ((r<=96) && (r>=91)) || ((r<=175) && (r>=123)) || ((r<=254) && (r>=178))){

    random = r;

    break;

    }

    }

    break;

    case 'e':

    random = (rand() % (126 + 1 - 33)) + 33;

    break;

    case 'f':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if (((r<=90) && (r>=65)) || ((r<=122) && (r>=97))){

    random = r;

    break;

    }

    }

    break;

    case 'g':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if (((r<=57) && (r>=48)) || ((r<=122) && (r>=97))){

    random = r;

    break;

    }

    }

    break;

    case 'h':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if (((r<=122) && (r>=97)) ||((r<=47) && (r>=33)) || ((r<=64) && (r>=58)) || ((r<=96) && (r>=91)) || ((r<=175) && (r>=123)) || ((r<=254) && (r>=178))){

    random = r;

    break;

    }

    }

    break;

    case 'i':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if (((r<=90) && (r>=65)) || ((r<=57) && (r>=48))){

    random = r;

    break;

    }

    }

    break;

    case 'j':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if (((r<=90) && (r>=65)) ||((r<=47) && (r>=33)) || ((r<=64) && (r>=58)) || ((r<=96) && (r>=91)) || ((r<=175) && (r>=123)) || ((r<=254) && (r>=178))){

    random = r;

    break;

    }

    }

    break;

    case 'k':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if (((r<=57) && (r>=48)) || ((r<=47) && (r>=33)) || ((r<=64) && (r>=58)) || ((r<=96) && (r>=91)) || ((r<=175) && (r>=123)) || ((r<=254) && (r>=178))){

    random = r;

    break;

    }

    }

    break;

    case 'l':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if(((r<=122) && (r>=97)) || ((r<=90) && (r>=65)) || ((r<=57) && (r>=48))){

    random = r;

    break;

    }

    }

    break;

    case 'm':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if(((r<=90) && (r>=65)) || ((r<=57) && (r>=48)) || ((r<=47) && (r>=33)) || ((r<=64) && (r>=58)) || ((r<=96) && (r>=91)) || ((r<=175) && (r>=123)) || ((r<=254) && (r>=178))){

    random = r;

    break;

    }

    }

    break;

    case 'n':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if(((r<=122) && (r>=97)) || ((r<=57) && (r>=48)) || ((r<=47) && (r>=33)) || ((r<=64) && (r>=58)) || ((r<=96) && (r>=91)) || ((r<=175) && (r>=123)) || ((r<=254) && (r>=178))){

    random = r;

    break;

    }

    }

    break;

    case 'o':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if(((r<=122) && (r>=97)) || ((r<=90) && (r>=65)) || ((r<=47) && (r>=33)) || ((r<=64) && (r>=58)) || ((r<=96) && (r>=91)) || ((r<=175) && (r>=123)) || ((r<=254) && (r>=178))){

    random = r;

    break;

    }

    }

    break;

    case 'p':

    while (1){

    r = (rand() % (256 + 1 - 0)) + 0;

    if(((r<=122) && (r>=97)) || ((r<=90) && (r>=65)) || ((r<=57) && (r>=48)) || ((r<=47) && (r>=33)) || ((r<=64) && (r>=58)) || ((r<=96) && (r>=91)) || ((r<=175) && (r>=123)) || ((r<=254) && (r>=178))){

    random = r;

    break;

    }

    }

    break;

    default:

    printf("Invalid Input so we are exiting you from program and starting again\n");

    main();

    }

    return random;

    }

    void printPassword(){

    int randomNumber;

    int k;

    int length = atoi(passwordLength);

    for (k = 0; k < length; k++) {

    randomNumber = generateRandomNumbers();

    password[k] = randomNumber;

    }

    for (int j = 0; j < length+19; j++) {

    printf("=");

    }

    printf("\n");

    printf("Your password is: %s", password);

    printf("\n");

    for (int a = 0; a < length+19; a++) {

    printf("=");

    }

    printf("\n");

    }