8+ C Password Validator Program Examples

This post contains a total of 8+ C Password Validator Program Examples with Source Code. All these programs to Validate Passwords are made using C Language.

You can use the source code of these examples with credits to the original owner.

Related Posts

C Password Validator Program Examples

1. By Dark

Made by Dark. Simple Password validator Program. Source

Password is [email protected]
Password is valid
#include <stdio.h>
#include<string.h>
int main() {
char s[10];
int sy=0,cap=0,num=0,i;
scanf("%s",s);
printf("Password is %s\n",s);
if(strlen(s)>5 && strlen(s)<10)
{
    for(i=0;i<strlen(s);i++)
    {
        if(s[i]>65 && s[i]<91)
          cap++;
        else if(s[i]>48 && s[i]<57)
          sy++;
        else if(s[i]<97 || s[i]>123)
          num++;
    }
    if(sy==0 || cap==0 || num==0){
    printf("A strong password must be between 5-10 charcters and must contain atleast 1 or more symbols,capital letters,numbers");}
    else
      printf("Password is valid");
}
else 
printf("Password is not between 5-10 characters");
    return 0;
}

2. By Aung Thiha

Made by Aung Thiha. C program to validate a password and test its strength. Source

[email protected]
Cool!!!You entered the strong password.
#include <stdio.h>     //making a in/out file
#include <string.h>    //string libraby
   int main( ){       // main function
   int l=5,h=15,v= 1; //declaring variable
   char n[10] = "0123456789" ;    
   char c[25] = "@#*ยฟ$ยฃโ‚ฌ?|%^`ยดยฐโ€ข\:<>~_&/+-ร—";/* 
     _____________________________________
    |    read this two lines. you can only|
    | add from those except of letters.   | 
     -------------------------------------
    */char password[h+1];
    gets(password);    
if((strlen(password)>h)||(strlen(password)<l)){
                v=0; }/*
     _______________________________________
    |    strlen is used detect the string's |
    |    lengths.Is it <5 or >15            |                        
     ---------------------------------------
     */char *temp = password;
if (strpbrk(temp,n) == NULL){
                v = 0;}
          temp = password;
if (strpbrk(temp,c) == NULL){
                v = 0;}/*               
 _____________________________________________               
|       strpbrk() is used to find the         |    | first character of first string and matches |
| it to any character of second string.It     | | returns NULL, if no matches are found       | | otherwise, it returns a pointer to the      | | character of first string that matches to   |
| the character of second string.             |    
 ---------------------------------------------                              
*/for(int i=0; password[i] != '\0'; i++){
    if(password[i] == ' '){
            v = 0;
            break;}}    
    (v)? printf("Cool!!!You entered the strong password. I hope your password would be safe.") 
    : printf("Oops!!!I guess, your password is weak or invalid.\n\nYou should type\n1. at least 5 words.\n2. less than 15 words.\n3. at least 1 or 2 special characters.\n4. at least 1 number.\n\nexample : \t\t\tthiha#1122");/*
   ___________________________________________
  |      (v) ? statement  : statment          |
  |  is ternary operator. it is like with     | 
  |  'if' condition operator...if(v) condition|
  |  is true statement is work. else condition|
  |  is wrong, statement won't work.          |
   ------------------------------------------- 
    */return 0;}

3. By Rabs

Made by Rabs. Source

[email protected]@99
Strong
#include <stdio.h>

int main() {

    char pass[20];
    char str1[]="Strong";
    char str2[]="Weak";
    int length=0,num=0,special=0,i=0;
    scanf("%s",pass);
    length=strlen(pass);
    while(pass[i] != '\0')
    {
        if(length>=7)
        {
            if(pass[i]>='0' && pass[i]<='9')
            {
                num++;
            }
            if(pass[i]=='!' || pass[i]=='@' || pass[i]=='#' || pass[i]=='$' || pass[i]=='%' || pass[i]=='*'|| pass[i]=='&')
            {
                special++;
            }
        }
      ++i;
    }
     if(num>=2 && special>=2)
    {
      printf("%s",str1);
    
    }
    else{
      printf("%s",str2);
    }
}

4. By Vikash Karma

Made by Vikash Karma. Source

[email protected]
True
#include<stdio.h>
#include<string.h>
int main(){
   int len,num,spe,space;
  int i;
    char s[15];

num=spe=space=0;
gets(s);

len=strlen(s);

  
if(len>10||len<5) 
   printf("False\n");

else{
  for(i=0;s[i];i++){
   if(s[i]>=48&&s[i]<=57)
     num++;

 else if(s[i] ==32)
     space++;

else if(!((s[i]>=48&&s[i]<=57)|| (s[i] ==32)||(s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122)))
   spe++;
}}
  if(num!=0&&space==0&&spe!=0)
   printf("True\n");

 else 
   printf("False\n");
   return 0;

}

5. By zhoolf

Made by zhoolf. Advanced Password validator Program. Source

[email protected]
True
#include <stdio.h>
#include <string.h>

#define MIN_LEN 6
#define MAX_LEN 30

#define DIGITS "0123456789"
#define SPECIAL_CHARS "&[email protected]%#$/-!^<>_()*-~?"

unsigned int isValid = 1;

int main()
{
    char input[MAX_LEN+1];
    gets(input);
    
    // check minimum and maximum length
    if ((strlen(input) < MIN_LEN) || (strlen(input) > MAX_LEN))
        isValid = 0;
    
    char *temp = input;
    // check if password contain at least one number
    if (strpbrk(temp, DIGITS) == NULL)
        isValid = 0;
    
    temp = input;
    // check if password contain at least one special char
    if (strpbrk(temp, SPECIAL_CHARS) == NULL)
        isValid = 0;
    
    // check if password contain spaces
    int i;
    for (i=0; input[i] != '\0'; i++) {
        if (input[i] == ' ') {
            isValid = 0;
            break;
        }
    }
        
    (isValid) ? printf("true") : printf("false");

    return 0;
}

6. By Ajai

Made by Ajai. Source

****Password validator****

Enter Your password: 12abc
Weak
#include<stdio.h>

int main()
{
    int i, count=0, count_num = 0, count_sc= 0;
    char str[100], ch;
    printf("****Password validator****\n\nEnter Your password:  ");
      gets(str);
      for(i=0; str[i]!='\0'; i++)
    {
            count++;
    }

    if(count <= 7 )  
    {
       printf("Weak");  //if password has less than 7 characters it is weak
       return 0;
    }
    else
    {   for(i=0; str[i] != '\0'; i++)
        {
            if (str[i] >= '0' && str[i] <='9')
                ++count_num;
        }
        if(count_num < 2)  //if password has less than 2 numbers it is weak
        {
          printf("Weak");
          return 0;
        }
        else
            for(i=0; str[i] != '\0'; i++)  //checking for special characers [email protected]#$%&*
        {
            if (str[i] == '!' || str[i] == '@' ||str[i] == '#' ||str[i] == '$' ||str[i] == '%' ||str[i] == '*' ||str[i] == '&')

                ++count_sc ;

        }
        if(count_sc < 2)  
        {
          printf("Weak");  //if password has less than 2 special characters it is weak
          return 0;
        }
        else printf("Strong");
        }
    return 0;
}

7. By HonFu

Made by HonFu. Source

Your input: qwerty

Your password needs at least one digit and one special character!
#include <stdio.h>

int is_valid_pw(char *pw) {
  int special=0, digit=0;
  for(int i=0; pw[i]!='\0'; ++i) {
    if (pw[i+1]=='\0'&&i<4) {
      printf("Your password is "
             "too short (<5)!\n");
      return 0;
    }
    if (i>9) {
      printf("Your password is "
             "too long (>10)!\n");
      return 0;
    }
    if (pw[i]<'!'||pw[i]>'~'||pw[i]=='\\') {
      printf("Please use only visible "
             "ASCII (except \\)!\n");
      return 0;
    }
    if (pw[i]>='0'&&pw[i]<='9'&&!digit)
      ++digit;
    else if (((pw[i]>=':'&&pw[i]<='@')
           || (pw[i]>='['&&pw[i]<=96)
           || (pw[i]>='{'&&pw[i])
           || pw[i]<='/')
           && !special)
      ++special;
  }
  if(!digit||!special) {
    printf("Your password needs at least "
           "one digit and one "
           "special character!\n");
    return 0;
  }
  return 1;
}

int main() {
  char str[101];
  printf("Your input: ");
  scanf("%[^\n]", str);
  printf("%s\n\n", str);
  if(is_valid_pw(str)) 
    printf("Valid!\n");
  return 0;
}

8. By Endeavourer

Made by Endeavourer. Source

w2w2
Weak Password!!
#include <stdio.h>
#include <string.h>
int main() {
    char str[30];
    int i,a=0,b=0,c=0;
    
    scanf("%s",str);
    
    for(i=0;str[i]!='\0';){
        if((str[i]>='0')&&(str[i]<='9')){
        a++;
        i++;
    }
    else if((str[i]>='!')&&(str[i]<='/')){
        b++;
        i++;
    }
    else if(str[i]==' '){
        c++;
        i++;
    }
    else {
        i++;
    }
    }  
    
    //here you can do little modification ๐Ÿ˜‰
    
     if((strlen(str)>=5)&&(strlen(str)<11)&&(a>=1)&&(b>=1)&&(c==0))
        printf ("Strong Password!!");
    else
    printf ("Weak Password!!");
    
    return 0;
}

9. By Miquel Andreu Fuster Sancho

Made by Miquel Andreu Fuster Sancho. Source

aaa: 0
aaabbbcccddd: 0
aaabbb: 0
aaa bbb: 0
aaa0bbb: 0
[email protected]: 0
aaa0!bbb: 1
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

bool validator(char *password) {
    size_t str_len = strlen(password);
    bool has_letters = false;
    bool has_numbers = false;
    bool has_symbols = false;

    if(str_len < 5 || str_len > 10)
        return false;

    for(int i=0; i<str_len; ++i) {
        if(isalpha(password[i]))
            has_letters = true;
        else if(isdigit(password[i]))
            has_numbers = true;
        else if(isgraph(password[i]))
            has_symbols = true;
        else // has spaces or non graphic chars
            return false;
    }

    return has_letters && has_numbers && has_symbols;
}

int main() {
    char *passwords[] = {"aaa", "aaabbbcccddd", "aaabbb", "aaa bbb", "aaa0bbb", "[email protected]", "aaa0!bbb"};
    for(int i = 0; i < sizeof(passwords)/sizeof(passwords[0]); ++i)
        printf("%s: %d\n", passwords[i], validator(passwords[i]));
}