Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #define TRUE 1
- #define FALSE 0
- int apply_password_policy(char password[], int len){
- int i,j,k=0;
- /* Policy1:
- * Space not allowed in the password
- */
- for (i=0; i<len; i++){
- if (password[i] == ' '){
- return FALSE;
- }
- }
- /* Policy2:
- * Password should be between 1 to 10 Uppercase letters
- */
- for (i=0; i<len; i++){
- if (!isupper(password[i])){
- return FALSE;
- }
- }
- /* Policy3:
- * Same character should not appear together.
- */
- for (i=0; i<len; i++) {
- if ( i+1 < len) {
- if (password[i] == password[i+1])
- return FALSE;
- }
- }
- /* Policy4:
- * Same string shold not appear together.
- */
- for (i=0; i<len; i++){
- for (j=i+1; j<len; j++) {
- if(password[i] == password[j]) {
- if ( k == 0) {
- /* store the J value when first character match happens
- */
- k=j;
- break;
- }
- /* Next subscript of 'i' is equal to the first charater match happens at 'J'
- * Meaning, substring is together.
- */
- if ( i+1 == k)
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- int main(){
- char password[10];
- int len=0;
- printf("Enter the password: ");
- while (1) {
- password[len]=getc(stdin);
- if (password[len]=='\n'){
- password[len]='\0';
- break;
- }
- len++;
- }
- if (apply_password_policy(password, len))
- printf ("Password Accepted. \n");
- else
- printf("Password Rejected. \n");
- return 0;
- }
Add Comment
Please, Sign In to add comment