// Program 5: Arrange Sentences by Word Count import java.util.*; class SentenceOrder { static int words(String s) { int count = 0; boolean inside = false; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != ' ') { if (!inside) { count++; } inside = true; } else { inside = false; } } return count; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String text = sc.nextLine(); String[] sentences = new String[10]; int[] totals = new int[10]; int size = 0; String part = ""; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (ch == '.' || ch == '!' || ch == '?') { if (part.trim().length() > 0 && size < 10) { sentences[size] = part.trim(); totals[size] = words(sentences[size]); size++; } part = ""; } else { part += ch; } } // Stable insertion sort by word count. for (int i = 1; i < size; i++) { String item = sentences[i]; int total = totals[i]; int j = i - 1; while (j >= 0 && totals[j] > total) { sentences[j + 1] = sentences[j]; totals[j + 1] = totals[j]; j--; } sentences[j + 1] = item; totals[j + 1] = total; } for (int i = 0; i < size; i++) { System.out.println(sentences[i] + " = " + totals[i]); } } }