6 Replies - 3528 Views - Last Post: 27 October 2012 - 09:09 PM Rate Topic: -----

#1 Doughboy123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-October 12

Reversing the letters in a word in a sentence with Java

Posted 02 October 2012 - 10:21 AM

Hello,

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Reversing the letters in a word in a sentence with Java

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Reversing the letters in a word in a sentence with Java

Posted 02 October 2012 - 10:28 AM

What do you mean. The punctuation/spaces aren't being added to the stack?
Was This Post Helpful? 0
  • +
  • -

#3 kellzor   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 12

Re: Reversing the letters in a word in a sentence with Java

Posted 02 October 2012 - 10:30 AM

are stacks required? bubble sorting would be easier for this i think
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Reversing the letters in a word in a sentence with Java

Posted 02 October 2012 - 10:33 AM

I'm not sure I agree. I think this kind of assignment is useful in teaching how stacks work and how they are used.
Was This Post Helpful? 0
  • +
  • -

#5 Doughboy123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-October 12

Re: Reversing the letters in a word in a sentence with Java

Posted 02 October 2012 - 10:36 AM

Right now a the reversed word sentence is printing like this: sihTsinaemosewa.yad
I want it to print like this:
sihT si na emosewa yad.

Also using a stack is required.
Was This Post Helpful? 0
  • +
  • -

#6 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,618
  • Joined: 25-December 08

Re: Reversing the letters in a word in a sentence with Java

Posted 02 October 2012 - 10:50 AM

String Tokenizer will default to tokenize on " ". So lets look at your operations. Tokenize and then push each on a stack will leave you with:
"This is an"
[0]n
[1]a
[2]s
[3]i
[4]s
[5]i
[6]h
[7]T

popping them off will give you nasisihT. Do you see your problem? You don't store the " ", they are thrown away. You need to push a " " after each word. In your current implementation you could just push(" ") after each token:
e.g.
for each token{
   for each character in token
   {
      push character
   }
   push " "
}

This post has been edited by SwiftStriker00: 02 October 2012 - 10:51 AM

Was This Post Helpful? 1
  • +
  • -

#7 Doughboy123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-October 12

Re: Reversing the letters in a word in a sentence with Java

Posted 27 October 2012 - 09:09 PM

Thanks SwiftStriker00 for the help. I was able to fix my mistake. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1