加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何验证密码在C中至少有一个大写,小写和数字?

发布时间:2020-12-16 10:25:13 所属栏目:百科 来源:网络整理
导读:我该怎么做才能使它保持循环,直到它至少有一个大写,小写和数字? 我卡住了,真的卡住了…… char password[100][15];i=1; printf("Password [3..10]: "); gets(password[i]); while (strlen(password[i])10 || strlen(password[i])3 || ) { do{ printf(" Pass
我该怎么做才能使它保持循环,直到它至少有一个大写,小写和数字?

我卡住了,真的卡住了……

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 numbernPassword [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,FILE *file) {
    printf("Password [3..10]: ");
    if(my_gets(buf,file) == 0) {
        return -1;
    }
    while(is_legal(buf) == 0) {
       printf("  Password must contain at least 1 uppercase,and 1    umbernPassword [3..10]: ");
       if(my_gets(buf,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 passwordn");
        exit(1);
   }
   return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读