import java.util.Scanner;

/**
 * ISC Computer Science Practical Examination
 * Topic: String Manipulation, Tokenization, Caesar Cipher & Custom Sorting
 * Class: StringCipherSort
 */
public class StringCipherSort {

    // Helper method to encrypt a single word using Caesar Cipher (+2 shift)
    public static String encryptWord(String word) {
        String enc = "";
        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                // Circular shift: (ch - 'A' + 2) % 26 + 'A'
                char shifted = (char) (((ch - 'A' + 2) % 26) + 'A');
                enc += shifted;
            } else {
                enc += ch;
            }
        }
        return enc;
    }

    // Helper method to check if a character is a vowel
    public static boolean isVowel(char ch) {
        return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U';
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("ENTER SENTENCE:");
        String raw = in.nextLine().trim();

        // 1. Validation Gate: Must not be empty and must terminate with ., ?, or !
        if (raw.isEmpty()) {
            System.out.println("INVALID INPUT");
            return;
        }

        char terminator = raw.charAt(raw.length() - 1);
        if (terminator != '.' && terminator != '?' && terminator != '!') {
            System.out.println("INVALID INPUT");
            return;
        }

        // 2. Strip terminator and convert to UPPERCASE
        String text = raw.substring(0, raw.length() - 1).trim().toUpperCase();
        if (text.isEmpty()) {
            System.out.println("INVALID INPUT");
            return;
        }

        // 3. Robust Tokenization using regex (collapses multiple consecutive spaces)
        String[] words = text.split("\\s+");
        if (words.length < 2) {
            System.out.println("INVALID INPUT");
            return;
        }

        // 4. Count Vowels and Consonants in original text
        int vowelCount = 0;
        int consonantCount = 0;
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                if (isVowel(ch)) {
                    vowelCount++;
                } else {
                    consonantCount++;
                }
            }
        }

        // 5. Caesar Cipher Encryption (+2 circular shift)
        String[] encrypted = new String[words.length];
        for (int i = 0; i < words.length; i++) {
            encrypted[i] = encryptWord(words[i]);
        }

        // 6. Custom Sorting: Length Priority, Alphabetical Tie-Breaker (Bubble Sort)
        for (int i = 0; i < encrypted.length - 1; i++) {
            for (int j = 0; j < encrypted.length - 1 - i; j++) {
                int lenA = encrypted[j].length();
                int lenB = encrypted[j + 1].length();
                boolean shouldSwap = false;

                if (lenA > lenB) {
                    shouldSwap = true;
                } else if (lenA == lenB) {
                    if (encrypted[j].compareTo(encrypted[j + 1]) > 0) {
                        shouldSwap = true;
                    }
                }

                if (shouldSwap) {
                    String temp = encrypted[j];
                    encrypted[j] = encrypted[j + 1];
                    encrypted[j + 1] = temp;
                }
            }
        }

        // 7. Reassemble transformed sentence with original terminator
        String result = "";
        for (int i = 0; i < encrypted.length; i++) {
            result += encrypted[i];
            if (i < encrypted.length - 1) {
                result += " ";
            }
        }
        result += terminator;

        // Display Outputs
        System.out.println("\nORIGINAL SENTENCE:");
        System.out.println(raw.toUpperCase());

        System.out.println("\nTRANSFORMED SENTENCE:");
        System.out.println(result);

        System.out.println("\nVOWELS: " + vowelCount);
        System.out.println("CONSONANTS: " + consonantCount);
    }
}
