字符C程序

时间:2018-12-22 18:54:00

标签: c

/ 编写程序以检查密码是否包含大写字母,数字和特殊符号。 (我已经提供了我已经做过的事情,但是我没有得到所需的输出。在我弄乱地方的地方需要解释。) /

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

int main()
{
  int i;
  int tuna;
  do 
  {
    printf("Enter a password:%c \n", tuna);
    scanf(" %c", &tuna);

  } while( tuna != (isupper(tuna), isalpha(tuna), isdigit(tuna)) );


  return 0;
}

2 个答案:

答案 0 :(得分:-1)

这是您问题的完整解决方案。

您需要创建一个定义特殊字符的函数。

此外,您需要以字符串形式读取密码,然后将每个字符与密码规则进行比较。

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

bool isspecial(char c)
{
    return ((c >= 0x21) && (c <= 0x2F)) ||
           ((c >= 0x3A) && (c <= 0x40)) ||
           ((c >= 0x5B) && (c <= 0x60)) ||
           ((c >= 0x7B) && (c <= 0x7E));
}

int main(void)
{
    bool contains_upper = false;
    bool contains_digit = false;
    bool contains_special = false;

    int i = 0;

    char tuna[65] = { 0x00 }; // password max length is 64 chars

    printf("Enter a password: ");
    fgets(tuna, 64, stdin);

    tuna[strlen(tuna) - 1] = 0x00;

    while (i < strlen(tuna))
    {
        if (isupper(tuna[i]))
        {
            contains_upper = true;
        }

        if (isdigit(tuna[i]))
        {
            contains_digit = true;
        }

        if (isspecial(tuna[i]))
        {
            contains_special = true;
        }

        i++;
    }

    if (contains_upper && contains_digit && contains_special)
    {
        printf("Password is ok\n");
    }
    else
    {
        printf("Password is not ok\n");
    }

    return 0;
}

答案 1 :(得分:-3)

https://onlinegdb.com/rkuWbGhx4

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

#define PASSLENGTH 32

char *readpass(void)
{
    int c;

    static char password[PASSLENGTH];
    size_t len = 0;

    while((c = getch()) != '\n')
    {
        if(c < 32 || c > 127)
        {
            continue;
        }
        else
        {
            if(len == PASSLENGTH -1) continue;
            password[len++] = c;
            printf("*");
        }
    }
    password[len] = 0;
    return password;
}

int isspecial(int c)
{
    return !!strchr("*&^%$#()@!", c);
}

int checkpass(const char *pass)
{
    int digits = 0, upper = 0, special = 0;; 
    while(*pass)
    {
        digits |= isdigit(*pass);
        upper |= isupper(*pass);
        special |= isspecial(*pass);
        pass++;
    }
    return digits && upper && special;
}

int main()
{
    char *password;
    int result;

    do
    {
        password = readpass();
        result = checkpass(password);

        if(!result) printf("\nWrong password\n");
    }while(!result);

    printf("\nYour password is: %s\n", password);
    return 0;
}