Code:
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);
fixOrder(reverse);
String[] newSentence = fixOrder(reverse);
System.out.println("Reversing the words in the sentence: " + original + ", looks like: " + newSentence );
}
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 String[] fixOrder(String reverse){
String arrayList[] = {reverse};
String[] result = arrayList;
for (int index = arrayList.length-1; index >= 0; index--)
result = arrayList;
return result;
}
}
when i run the program it states: Reversing the words in the sentence: hello world, looks like: [Ljava.lang.String;@5304f889
so if a user types: I am enjoying this the output would be i ma gniyojne siht