So im trying to reverse the words in a sentence like: "Hi dog cat", would become "iH god tac". I was wondering if i can use what im doing to achieve that. I can get the sentence itself to reverse, but i cant get individual words to do so. Is there a way to do this with Strings or do i have to mess with Character(which is confusing too)? Any help is appreciated
private static String PrintStack(String sentence)
{
String reverse = "";
String stringReversed = "";
String Last;
String First;
Stack<String> stack= new Stack<String>();
String words[] = sentence.split(" ");
Last = words[words.length-1];
for(int j = 0; j < words.length; j++)
{
String newWord = words[0+j];
stack.push(newWord);
System.out.println(stack);
}
while(!stack.isEmpty())
{
stringReversed += stack.pop();
}
System.out.println("Reverse is: " + stringReversed);
return reverse;
}
}