public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the word reversing program");
System.out.println("First you will enter a sentence and then the letters of each word will be in the reversed order");
System.out.print("Please enter a Sentence: ");
String original = input.nextLine();
wordReverse(original);
String reverse = wordReverse(original);
List<String> nSentence = fixOrder(reverse);
System.out.println("Reversing the words in the sentence: " + original + ", looks like: " + nSentence );
}
public static String wordReverse(String original){
StringTokenizer string = new StringTokenizer(original);
Stack<Character> charStack = new Stack<Character>();
while (string.hasMoreTokens()){
String stack = string.nextToken();
for (int i = 0; i < stack.length(); i ++){
charStack.push(stack.charAt(i));
}
charStack.push(' ');
}
StringBuilder result = new StringBuilder();
while(!charStack.empty()){
result.append(charStack.pop());
}
return result.toString();
}
public static List<String> fixOrder(String reverse){
List<String> list = Arrays.asList(reverse);
Collections.reverse(list);
return list;
}
}