c - C语言如何验证密码至少有一个大写、小写和数字?

标签 c passwords uppercase lowercase

我应该怎么做才能让它一直循环直到它至少有一个大写字母、小写字母和数字?

我卡住了,真的卡住了......

char password[100][15];
i=1;
     printf("Password [3..10]: ");
        gets(password[i]);
        while (strlen(password[i])>10 || strlen(password[i])<3 || ) {   
        do{
        printf("  Password must contain at least 1 uppercase, 1 lowercase, and 1 number\nPassword [3..10]: ");
        gets(password[i]);
        } while (strlen(password[i])>10 || strlen(password[i])<3 );

最佳答案

这应该有效:

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

int is_legal(char *p) {
  int number = 0, lower = 0, upper = 0, length = 0;
  for( ; *p; p++) {
      number += isdigit(*p);   
      lower  += islower(*p);     
      upper  += isupper(*p);
      length++;
  }
  return number > 0 && lower > 0 && upper > 0  && length > 3 && length < 10;
}

char *my_gets(char *buf, int bufsize, FILE *file) {
    if(fgets(buf, bufsize, file) == 0) {
        return 0;
    }
    int n = strlen(buf);
    if(buf[n-1] == '\n') buf[n-1] = 0;
    return buf;
}

int get_password(char *buf, int bufsize, FILE *file) {
    printf("Password [3..10]: ");
    if(my_gets(buf, bufsize, file) == 0) {
        return -1;
    }
    while(is_legal(buf) == 0) {
       printf("  Password must contain at least 1 uppercase, 1 lowercase, and 1    umber\nPassword [3..10]: ");
       if(my_gets(buf, bufsize, file) == 0) {
           return -1;
       }
    }
    return 0;
}

int main(void) {
    char password[100][15];
    int i = 0;
    if(get_password(password[i], sizeof(password[i]), stdin) != 0) {
        fprintf(stderr, "Error getting password\n");
        exit(1);
   }
   return 0;
}

关于c - C语言如何验证密码至少有一个大写、小写和数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19661738/

相关文章:

c++ - 从 Amazon FreeRTOS 的串行问题中读取

c - 如何在C中读取一个整数后跟一个字符串?

c - 终端不允许我打印指向末尾没有换行符的字符串的指针 - C

linux - 如何设置root密码?

c - 如何使用命令行参数输入数据并将数据转换为大写

c - C 中的小写字符转换为大写字符并写入文件

c - 与 O_CREAT|O_EXCL 相反

c# - 正则表达式在 C# Azure 函数中包含一个(小写、大写、数字、给定的特殊字符)

mysql - ProFTPD 如何读取使用 MySQL ENCRYPT() 函数加密的密码?

javascript - 大写文本输入的第一个字符