In: Computer Science
Using Java:
Write a program that uses hash tables and reads in a string from the command line and a dictionary of words from standard input, and checks whether it is a "good" password. Here, assume "good" means that it (i) is at least 8 characters long, (ii) is not a word in the dictionary, (iii) is not a word in the dictionary followed by a digit 0-9 (e.g., hello5), (iv) is not two words separated by a digit (e.g., hello2world).
import java.util.*;
import java.lang.*;
public class x{
public static void main(String[] args) {
//Intializing a HashTable
Hashtable<String, Integer> h
= new Hashtable<String,Integer>();
Scanner s=new
Scanner(System.in);
System.out.println("Enter how many
words to add in dictionary:");
int dictSize=s.nextInt();
//Filling the Dictionary
for(int
i=0;i<dictSize;i++){
System.out.println("Enter dictionary Word:");
String
word=s.next();
h.put(word,new
Integer(1));
}
//Input a string
System.out.println("Enter
Password:");
String password=s.next();
//Check for password size
if(password.length()<8){
System.out.println("Not a Good Password,String length less than
8");
System.exit(0);
}
//Check if password is one of
dictionary word
if(h.containsKey(password)){
System.out.println("Not a Good Password,String present in
dictionary");
System.exit(0);
}
//To extract the part before
digit
String letterPart="";
for(int
i=0;i<password.length();i++){
char
as=password.charAt(i);
if(as>='0'
&& as<='9'){
letterPart=password.substring(0,i);
//If any letter is present after number then not
a good password
if(i!=(password.length()-1)){
System.out.println("Not a
Good Password,Digit present between words");
System.exit(0);
}
}
}
//Check if password is not a
dictionary word followed by digit
if(h.containsKey(letterPart)){
System.out.println("Not a Good Password,String present in
dictionary");
System.exit(0);
}
System.out.println("Good
Password!!");
}
}