Simple password verifier program

To be acceptable, a password must satisfy these three rules:

1. It must contain at least one vowel.
2. It cannot contain three consecutive vowels or three consecutive consonants.
3. It cannot contain two consecutive occurrences of the same letter, except for ‘ee’ or ‘oo’.

(For the purposes of this problem, the vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’; all other letters are consonants.) Note that these rules are not perfect; there are many common/pronounceable words that are not acceptable.

The input consists of one or more potential passwords, one per line, followed by a line containing only the word ‘end’ that signals the end of the file. Each password is at least one and at most twenty letters long and consists only of lowercase letters. For each password, output whether or not it is acceptable, using the precise format shown in the example.

Example input:


Example output:

<a> is acceptable.
<tv> is not acceptable.
<ptoui> is not acceptable.
<bontres> is not acceptable.
<zoggax> is not acceptable.
<wiinq> is not acceptable.
<eep> is acceptable.
<houctuh> is acceptable.

So, the idea is to read a password, check if its valid(3 rules to check) and print if its acceptable or not.

Step 1: Check if the string contains a vowel, if it does, return true.

public boolean containsVowel()
 for (char c : password.toCharArray()) {
 if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
 return true;
 return false;

Step 2: Check if there are 3 consecutive vowels or consonants, if yes, return true, else returns false

public boolean consecVowelsOrConsontants() {
 int vc = 0;// consec vowels
 int cc = 0;// consec consonants
 boolean prevcharisvowel = true;
 for (char c : password.toCharArray()) {
 if (isVowel(c)) // if char is a vowel
 if (prevcharisvowel) // and the prev char was a vowel too
 vc++; // then increment
 else // else this was the first vowel
 vc = 1;
 prevcharisvowel = true;
 } else// if consonant
 if (prevcharisvowel)// and prev char was vowel
 cc = 1; // this is the first consonant
 prevcharisvowel = false;
 } else
 // else increment consecutive consonant count

 if (vc == 3 || cc == 3)
 return true;
 return false;

Step 3: check if 2 consecutive characters are the same, if yes, return true, Exception case: ee and oo are allowed

public boolean consecOccurances() {
 char prev;
 char next;
 if (password.length() < 2)
 return false;
 for (int i = 0; i < password.length() - 1; i++) {
 prev = password.charAt(i);
 next = password.charAt(i + 1);
 if (prev == next && !exceptionRule(prev, next))
 return true;
 return false;

 private boolean exceptionRule(char prev, char next) {
 StringBuffer s = new StringBuffer();

 if (s.toString().equals("ee") || s.toString().equals("oo"))
 return true;
 return false;

Put the 3 steps together

PasswordVerifier verify = new PasswordVerifier(str);

if(verify.containsVowel() && !verify.consecOccurances() && !verify.consecVowelsOrConsontants() )
 result= "<" + str + ">" + "is acceptable\n";
 result= "<" + str + ">" + "is not acceptable\n";

The code is not well formatted although I used “sourcecode” tags, so please excuse the poor formatting. Also, its always good to see better implementations of the above solution. I wrote the program in about 90 minutes, so it’d also be interesting to see if others can do it faster.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s