import java.io.PrintStream; import java.util.Scanner; public class Kangaroo { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.print("Enter two words, or Ctrl-d to quit: "); for (; input.hasNext(); ) { String kangaroo = input.next().toLowerCase(); String joey = input.next().toLowerCase(); StringBuffer result = new StringBuffer(kangaroo); boolean isKangaroo = true; int prevIndex = 0; for (int i = 0; i < joey.length() && isKangaroo; i++) { char c = joey.charAt(i); int index = kangaroo.indexOf(c, prevIndex); if (index != -1) { result.setCharAt(index, Character.toUpperCase(c)); prevIndex = index + 1; } else { isKangaroo = false; } } if (isKangaroo) { output.printf("%s is a kangaroo word%n%n", result, joey); } else { output.println("not a kangaroo word\n"); } output.print("Enter two words, or Ctrl-d to quit: "); } } }