# Problem Reversing String

Page 1 of 1

## 12 Replies - 2532 Views - Last Post: 08 October 2012 - 05:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294727&amp;s=395d1e241583d9a1e2db5d3d2d51fcea&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rbk3ai

Reputation: -4
• Posts: 64
• Joined: 08-September 12

# Problem Reversing String

Posted 08 October 2012 - 05:44 AM

Hey im having a problem reversing string still dont have all the concepts down concrete have order a book to help seal the deal with this material, but here is the problem.

/*Complete the ReverseString program below so that it reads in a string of characters, reverses the order of the characters, and prints out the reversed string.

In other words, if the input string is "123456", the output string would be "654321".
If the input string is "How now, brown cow", the output string would be "woc nworb ,won woH".
*/

import java.util.Scanner;

/**
Reads a string, reverses the order the characters within the
string, and prints out the result.
Input: the value of s, a string
Output: the string reversed
*/
public class ReverseString
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s = in.nextLine();

StringBuffer s = new StringBuffer
System.out.println("Before reversing the string :"+s);
s = s.reverse();
System.out.println("After Reverse string is :"+s);

}
}

Is This A Good Question/Topic? 1

## Replies To: Problem Reversing String

### #2 nick2price

• D.I.C Lover

Reputation: 565
• Posts: 2,826
• Joined: 23-November 07

## Re: Problem Reversing String

Posted 08 October 2012 - 05:51 AM

Are you allowed to use the reverse method? Normally when your doing it for school work, you have to do it manually. Doing it the way you do it now only shows you know how to call reverse. Either way, with the reverse method, just create a simple method
public String reverse(String s) {
return new StringBuffer(s).reverse().toString();
}

Now take the input from the user, and pass the string to this method. The problem with your code at the moment is your using the same variable name (s) for both your String and your StringBuffer.

### #3 rbk3ai

Reputation: -4
• Posts: 64
• Joined: 08-September 12

## Re: Problem Reversing String

Posted 08 October 2012 - 06:06 AM

I'm not too sure if we are allowed to... the teacher doesnt make anything CLEAR! I bought a book and i'm trying to teach myself. I gave up trying to learn from the teacher its like the teachers that go from algebra 1 to geometry than back to algebra 2 half ass education system if ya ask me...

This post has been edited by rbk3ai: 08 October 2012 - 06:10 AM

### #4 nick2price

• D.I.C Lover

Reputation: 565
• Posts: 2,826
• Joined: 23-November 07

## Re: Problem Reversing String

Posted 08 October 2012 - 06:25 AM

That method is doing the same thing as your doing now, but in a tidy way. The reason you are having problems at the moment is because your variable names are the same. Sort your variable names out so you dont have two called s.

### #5 lilVaratep

• D.I.C Regular

Reputation: 36
• Posts: 285
• Joined: 09-October 11

## Re: Problem Reversing String

Posted 08 October 2012 - 06:50 AM

You can use a loop to store them in an array starting from the last location of the string (size)

and then store that array into a string.

This post has been edited by lilVaratep: 08 October 2012 - 06:59 AM

### #6 Xente

Reputation: 3
• Posts: 36
• Joined: 31-January 12

## Re: Problem Reversing String

Posted 08 October 2012 - 08:57 AM

To make it easier you can use the split method on the string to store each number in an array and then iterate over the array in reverse order.

### #7 pbl

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

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Problem Reversing String

Posted 08 October 2012 - 03:17 PM

You are wasting our time
String s = in.nextLine();

StringBuffer s = new StringBuffer

You miss the parameter to the StingBuffer constructor

### #8 rbk3ai

Reputation: -4
• Posts: 64
• Joined: 08-September 12

## Re: Problem Reversing String

Posted 08 October 2012 - 04:58 PM

pbl, on 08 October 2012 - 03:17 PM, said:

You are wasting our time
String s = in.nextLine();

StringBuffer s = new StringBuffer

You miss the parameter to the StingBuffer constructor

sorry my code wont even "compiled" maybe next time I will do it right and make it to were it will compile correctly for you... also your time means nothing to me! so if you dont like my thread or whats in it or dont like the fact im a complete noob and the material is moving way fast than go elsewhere and respond on someone elses thread FFS!!!

### #9 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,717
• Joined: 13-March 10

## Re: Problem Reversing String

Posted 08 October 2012 - 05:06 PM

It is your freaking homework not ours. Maybe programming is not for you.
http://www.dreaminco...mework-request/

### #10 rbk3ai

Reputation: -4
• Posts: 64
• Joined: 08-September 12

## Re: Problem Reversing String

Posted 08 October 2012 - 05:06 PM

i already got it correct douche bag see ya go troll somewhere else... thanks lilvara i ended up using a for loop and most def shout out to BURAK! thanks bro

This post has been edited by rbk3ai: 08 October 2012 - 05:10 PM

### #11 nick2price

• D.I.C Lover

Reputation: 565
• Posts: 2,826
• Joined: 23-November 07

## Re: Problem Reversing String

Posted 08 October 2012 - 05:08 PM

You seriously pm'ing me for help and you have the cheek to talk to people like that? Sort your attitude out and then you may get people helping you. In the meantime, good luck with your work.

### #12 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,717
• Joined: 13-March 10

## Re: Problem Reversing String

Posted 08 October 2012 - 05:10 PM

LoL

### #13 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11787
• Posts: 44,294
• Joined: 27-December 08

## Re: Problem Reversing String

Posted 08 October 2012 - 05:15 PM

I'm closing this thread for obvious reasons. Personal attacks will not be tolerated.