Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem :
- Some computer programs try to prevent easily guessable passwords being used, by rejecting those that do not meet some simple rules. One simple rule is to reject passwords which contain any sequence of letters immediately followed by the same sequence.
- For example:
- APPLE Rejected (P is repeated)
- CUCUMBER Rejected (CU is repeated)
- ONION Accepted (ON is not immediately repeated)
- APRICOT Accepted
- Write a program which takes a password string as input (between 1 and 10 upper case letters) and then prints 'Rejected' if the password is rejected, or 'Accepted' otherwise. Your program should then terminate.
- Answer:
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #include<ctype.h>
- void verifyPassword(char *s);
- int static error=0;
- void main()
- {
- char pwd[10];
- clrscr();
- do
- {
- error=0;
- printf("\nEnter Password:");
- scanf("%s",pwd);
- //Verifying length of password (1-10)
- if(strlen(pwd)>10)
- {
- printf("\nPassword Rejected:Length more than 10 characters");
- error=1;
- }
- for(int k=0;k<strlen(pwd);k++)
- {
- //Verifying password is in uppercase
- if(isupper(pwd[k])==0)
- {
- printf("\nPassword Rejected:Enter password in uppercase letters\n");
- error=1;break;
- }
- }
- verifyPassword(pwd);
- }while(error==1);
- printf("\nPassword Accepted");
- getch();
- }
- void verifyPassword(char *s)
- {
- char a[1],b[1];
- //Verifying password has repeated characters
- for (int i=0;i<strlen(s);i++)
- {
- if(s[i]==s[i+1])
- {
- printf("\nPassword Rejected :%c repeated in password",s[i]);
- error=1;
- }
- }
- //Verifying password has string of size 2 repeated consecutively
- for(int j=0;j<strlen(s);j++)
- {
- a[0]=s[j];
- a[1]=s[j+1];
- b[0]=s[j+2];
- b[1]=s[j+3];
- if((a[0]==b[0]) && (a[1]==b[1]))
- {
- printf("\n Password Rejected: %c%c string repeated consecutively",a[0],a[1]);
- error=1;
- }
- }
- }
Add Comment
Please, Sign In to add comment