7 Replies - 4571 Views - Last Post: 24 October 2009 - 02:25 AM Rate Topic: -----

#1 shruthi87_bg@rediffmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-October 09

how to reverse a string in java

Post icon  Posted 22 October 2009 - 06:45 PM

how to reverse a string using java
Is This A Good Question/Topic? 0
  • +

Replies To: how to reverse a string in java

#2 d0ulikethat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 22-March 09

Re: how to reverse a string in java

Posted 22 October 2009 - 07:44 PM

Use a stack to push the character onto the stack. Then pop the characters off one by one.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,185
  • Joined: 06-March 08

Re: how to reverse a string in java

Posted 22 October 2009 - 08:36 PM

Or put the String into a StringBuffer which has a reverse() method

http://java.sun.com/...ringBuffer.html
Was This Post Helpful? 0
  • +
  • -

#4 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: how to reverse a string in java

Posted 22 October 2009 - 10:31 PM

or just read the string and make a for loop, using the for loop you will iterate through the string backwards

String s =;//"User's input"

for(int i = s.length()-1; i > 0; i--) {
	System.out.print(s.charAt(i));
}



This is probably more around the level of code your looking for... Good luck, if you need things explained just ask
Was This Post Helpful? 1
  • +
  • -

#5 leska  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 17-November 08

Re: how to reverse a string in java

Posted 23 October 2009 - 02:35 AM

Good solution is there.
Was This Post Helpful? 0
  • +
  • -

#6 shruthi87_bg@rediffmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-October 09

Re: how to reverse a string in java

Posted 23 October 2009 - 07:36 PM

View Postvirgul, on 22 Oct, 2009 - 09:31 PM, said:

or just read the string and make a for loop, using the for loop you will iterate through the string backwards

String s =;//"User's input"

for(int i = s.length()-1; i > 0; i--) {
	System.out.print(s.charAt(i));
}



This is probably more around the level of code your looking for... Good luck, if you need things explained just ask

I tried the program, I got some errors.,I think I went wrong in declaration........Can you please send the complete program........I just want to compare and rectify my mistakes
Was This Post Helpful? 0
  • +
  • -

#7 Vestah  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 86
  • Joined: 15-October 09

Re: how to reverse a string in java

Posted 24 October 2009 - 01:40 AM

Here is an example of how you can use StringBuffer to reverse a string
String myString = "HELLO"; //Input
StringBuffer sb = new StringBuffer(myString);
String reverse = sb.reverse().toString();
System.out.println(reverse);


@virgul: Your code does not read the last character of the string. You need to use i >= 0 instead of i > 0.
for(int i = myString.length()-1; i >= 0; i--) {

Was This Post Helpful? 0
  • +
  • -

#8 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Re: how to reverse a string in java

Posted 24 October 2009 - 02:25 AM

I agree with virgul.
Another thing you could do is convert the string to a character array and then use a for loop to access each element of the array and print them out.
Good Luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1