Advertisement
Lirbo

C Q1

Jan 26th, 2023 (edited)
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4.  
  5. #define MIN_PASSWORD_LENGTH 5
  6.  
  7. int get_password_length();
  8. bool get_password_number();
  9. bool get_password_capital();
  10. bool get_password_sign();
  11.  
  12. int main()
  13. {
  14.     char password[10];
  15.     char c;
  16.     int i = 0;
  17.     while((c = getchar()) != '\n')
  18.     {
  19.         password[i++] = c;
  20.     }
  21.     password[i] = '\0'; // Determines null by the end of the string.
  22.    
  23.     int passwordLength = get_password_length(password);
  24.     printf("Password Length: %d\n", passwordLength);
  25.    
  26.     if(passwordLength < MIN_PASSWORD_LENGTH)
  27.     {
  28.         printf("Password must contain at least 5 characters!");
  29.         return 0;
  30.     }
  31.    
  32.     if(!get_password_number(password, passwordLength))
  33.     {
  34.         printf("Password must contain a number!");
  35.         return 0;
  36.     }
  37.    
  38.     if(!get_password_capital(password, passwordLength))
  39.     {
  40.         printf("Password must contain a capital letter!");
  41.         return 0;
  42.     }
  43.    
  44.     if(!get_password_sign(password, passwordLength))
  45.     {
  46.         printf("Password must contain a special sign!");
  47.         return 0;
  48.     }
  49.    
  50.     printf("Your password meets all of the requirements!");
  51.    
  52.    
  53. }
  54.  
  55. int get_password_length(char* password)
  56. {
  57.     int length = 0;
  58.     char* p = password;
  59.     while (*p != '\0')
  60.     {
  61.         length++;
  62.         p++;
  63.     }
  64.     return length;
  65. }
  66.  
  67. bool get_password_number(char* password, int passwordLength)
  68. {
  69.     char* p = password;
  70.     for(int i = 0; i < passwordLength; i++)
  71.     {
  72.         if(*p >= '0' && *p <= '9')
  73.             return true;
  74.         p++;
  75.     }
  76.     return false;
  77. }
  78.  
  79. bool get_password_capital(char* password, int passwordLength)
  80. {
  81.     char* p = password;
  82.     for(int i = 0; i < passwordLength; i++)
  83.     {
  84.         if(*p >= 'A' && *p <= 'Z')
  85.             return true;
  86.         p++;
  87.     }
  88.     return false;
  89. }
  90.  
  91. bool get_password_sign(char* password, int passwordLength)
  92. {
  93.     char* p = password;
  94.     for(int i = 0; i < passwordLength; i++)
  95.     {
  96.         if(*p >= '!' && *p <= '/' || // !"#$%&'()*+,-./
  97.         *p >= ':' && *p <= '@' || // :;<=>?@
  98.         *p >= '[' && *p <= '`' || // {|}~
  99.         *p >= '{' && *p <= '~')
  100.             return true;
  101.         p++;
  102.     }
  103.     return false;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement