Advertisement
dimipan80

Generate 3-Letter Words

Aug 17th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. /* Write a program to generate and print all 3-letter words consisting of given set
  2.  * of characters. For example if we have the characters 'a' and 'b',
  3.  * all possible words will be "aaa", "aab", "aba", "abb", "baa", "bab", "bba" and "bbb".
  4.  * The input characters are given as string at the first line of the input.
  5.  * Input characters are unique (there are no repeating characters). */
  6.  
  7. import java.util.Locale;
  8. import java.util.Scanner;
  9.  
  10. public class _02_Generate3_LetterWords {
  11.  
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.         Locale.setDefault(Locale.ROOT);
  15.         Scanner scan = new Scanner(System.in);
  16.         System.out.println("Enter your letters in 1 String, without spaces:");
  17.         String givenLetters = scan.next();
  18.  
  19.         boolean isValidLetters = checkAllGivenCharactersIsValidLetters(givenLetters);
  20.         if (isValidLetters) {
  21.             int i1, i2, i3;
  22.             System.out.println("All 3-letter words consisting of given characters are:");
  23.             for (i1 = 0; i1 < givenLetters.length(); i1++) {
  24.                 for (i2 = 0; i2 < givenLetters.length(); i2++) {
  25.                     for (i3 = 0; i3 < givenLetters.length(); i3++) {
  26.                         printTheThreeLettersWord(givenLetters, i1, i2, i3);
  27.                     }
  28.                 }
  29.             }
  30.         } else {
  31.             System.out.println("Error! - Invalid Input!!!");
  32.         }
  33.     }
  34.  
  35.     private static boolean checkAllGivenCharactersIsValidLetters(String chars) {
  36.         for (int i = 0; i < chars.length(); i++) {
  37.             char symbol = chars.charAt(i);
  38.  
  39.             // Checking characters is alphabetical letters:
  40.             if (!Character.isLetter(symbol)) {
  41.                 return false;
  42.             }
  43.  
  44.             // Check current letter for repeating:
  45.             if (i > 0) {
  46.                 for (int j = i - 1; j >= 0; j--) {
  47.                     if (chars.charAt(j) == symbol) {
  48.                         return false;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.  
  54.         return true;
  55.     }
  56.  
  57.     private static void printTheThreeLettersWord(String givenLetters, int i1,
  58.             int i2, int i3) {
  59.         StringBuilder outputWord = new StringBuilder();
  60.         outputWord.append(givenLetters.charAt(i1));
  61.         outputWord.append(givenLetters.charAt(i2));
  62.         outputWord.append(givenLetters.charAt(i3));
  63.  
  64.         System.out.print(outputWord + " ");
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement