dimanche 1 octobre 2017

Password verification program in C

I'm trying to verify if the password entered by the user contains a letter, a number, and $ sign. Could you please take a look at the code below and let me know if I am missing out something since even if I enter a valid password it prints the else statement for an invalid password. PS: the program is written in C.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{

    char pass[10];
    int size = sizeof(pass);
    int i, a, b, c;

    printf("Enter password containing a Number, an Uppercase letter and a $ sign\n");
    scanf(" %c", pass);

    for(i = 0; i <= size; i++){
        if(isalpha(pass[i])){
                a = 1;
        }

        if(isdigit(pass[i])){
                b = 1;
        }

        if(pass[i] == '$'){
                c = 1;
        }

    }


    if(a == 1 && b == 1 && c == 1){
        printf("Password is valid\n");
    }else{
        printf("Password must contain at least a letter, a number and $\n");
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire