001package algs25; 002import stdlib.*; 003import java.util.Arrays; 004/* *********************************************************************** 005 * Compilation: javac Rhymer.java StdIn.java 006 * Execution: java Rhymer < data.txt 007 * 008 * Reads in a sequence of words. 009 * 010 *************************************************************************/ 011 012public class XRhymer { 013 014 015 private static String reverse(String s) { 016 StringBuffer sb = new StringBuffer(s); 017 sb.reverse(); 018 return new String(sb); 019 } 020 021 022 public static void main(String[] args) { 023 String input = StdIn.readAll(); 024 String[] strings = input.split("\\s+"); 025 026 // reverse order of letters in each word 027 for (int i = 0; i < strings.length; i++) 028 strings[i] = reverse(strings[i]); 029 030 // sort the words 031 Arrays.sort(strings); 032 033 // reverse order of letters in each word 034 for (int i = 0; i < strings.length; i++) 035 strings[i] = reverse(strings[i]); 036 037 // print words 038 for (String string : strings) 039 StdOut.println(string); 040 } 041}