Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to generate and print all 3-letter words consisting of given set
- * of characters. For example if we have the characters 'a' and 'b',
- * all possible words will be "aaa", "aab", "aba", "abb", "baa", "bab", "bba" and "bbb".
- * The input characters are given as string at the first line of the input.
- * Input characters are unique (there are no repeating characters). */
- import java.util.Locale;
- import java.util.Scanner;
- public class _02_Generate3_LetterWords {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter your letters in 1 String, without spaces:");
- String givenLetters = scan.next();
- boolean isValidLetters = checkAllGivenCharactersIsValidLetters(givenLetters);
- if (isValidLetters) {
- int i1, i2, i3;
- System.out.println("All 3-letter words consisting of given characters are:");
- for (i1 = 0; i1 < givenLetters.length(); i1++) {
- for (i2 = 0; i2 < givenLetters.length(); i2++) {
- for (i3 = 0; i3 < givenLetters.length(); i3++) {
- printTheThreeLettersWord(givenLetters, i1, i2, i3);
- }
- }
- }
- } else {
- System.out.println("Error! - Invalid Input!!!");
- }
- }
- private static boolean checkAllGivenCharactersIsValidLetters(String chars) {
- for (int i = 0; i < chars.length(); i++) {
- char symbol = chars.charAt(i);
- // Checking characters is alphabetical letters:
- if (!Character.isLetter(symbol)) {
- return false;
- }
- // Check current letter for repeating:
- if (i > 0) {
- for (int j = i - 1; j >= 0; j--) {
- if (chars.charAt(j) == symbol) {
- return false;
- }
- }
- }
- }
- return true;
- }
- private static void printTheThreeLettersWord(String givenLetters, int i1,
- int i2, int i3) {
- StringBuilder outputWord = new StringBuilder();
- outputWord.append(givenLetters.charAt(i1));
- outputWord.append(givenLetters.charAt(i2));
- outputWord.append(givenLetters.charAt(i3));
- System.out.print(outputWord + " ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement