I am writing a program to reverse the letters in an individual word in a sentence.
For example: Original input = This is an awesome day.
Reversed input = sihT si na emosewa yad.
I am using String Tokenizer to break the input up into tokens. I am then breaking the token up into characters and pushing those characters onto the stack. I am then popping the characters off the stack and then changing characters back to a string. I am having trouble putting spaces in between the reversed words and keeping punctuation at the end of words.
StringTokenizer st = new StringTokenizer("This is an awesome day.");
while (st.hasMoreTokens()){
String input = (st.nextToken());
int stackSize = input.length();
Stack theStack = new Stack(stackSize);
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
theStack.push(ch);
}
while (!theStack.isEmpty()) {
char ch = theStack.pop();
String str = String.valueOf(ch);
System.out.print(str);
}
}
Any help would be greatly appreciated.

New Topic/Question
Reply


MultiQuote




|