This is how I would do it:
public class ReverseString
{
public String reverse(String arg)
{
String tmp = null;
if (arg.length() == 1)
{
return arg;
}
else
{
//extract the last char
String lastChar = arg.substring(arg.length()-1,arg.length());
//extract the remaining chars
String remainingString = arg.substring(0, arg.length() -1);
tmp = lastChar + reverse(remainingString);
return tmp;
}
}
}
import java.io.*;
public class TestReverse
{
public static void main(String[] args) throws IOException
{
System.out.println("Enter a line to be reversed");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inData;
inData = br.readLine();
ReverseString rs = new ReverseString();
System.out.println("Reversed: " + rs.reverse(inData));
}
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
>This is how I would do it
You would use a recursive function for a problem clearly unsuited to a recursive solution? That's a shame.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>This is how I would do it
You would use a recursive function for a problem clearly unsuited to a recursive solution? That's a shame.
You have a better way?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
>You have a better way?
As a matter of fact, I do:
public String reverse ( String s )
{
StringBuffer buffer = new StringBuffer ( s );
return buffer.reverse().toString();
}
Though that probably won't work for homework. The obvious way is to use a loop, but because Strings are immutable you need to either do something stupid, or perform conversions. One conversion is using an array:
public String reverse ( String s )
{
int len = s.length();
char rev = new char[len];
for ( int i = 0; i < len; i++ )
rev[i] = s.charAt ( len - i - 1 );
return new String ( rev );
}
Both of these are considerably better than recursion, and I can think of at least half a dozen more variations on this theme.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>You have a better way?
As a matter of fact, I do:
public String reverse ( String s )
{
StringBuffer buffer = new StringBuffer ( s );
return buffer.reverse().toString();
}
Though that probably won't work for homework. The obvious way is to use a loop, but because Strings are immutable you need to either do something stupid, or perform conversions. One conversion is using an array:
public String reverse ( String s )
{
int len = s.length();
char rev = new char[len];
for ( int i = 0; i < len; i++ )
rev[i] = s.charAt ( len - i - 1 );
return new String ( rev );
}
Both of these are considerably better than recursion, and I can think of at least half a dozen more variations on this theme.
I guess your right. This is a much better way. At first I was thinking that although rucursion might not be the best answer but sometimes clarity and ease is the overriding concerne. Although your method was actually more clearer and easier to understand than mine.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
people don't you see that person is just learning ! ! !
Has no clue about buffers, streams, etc
think as beginners, not like overworked crazy programmers
for(int i = normal.length; i < 0; i--)
{
last = normal.charAt( i);
reversed += last;
}
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
Think like a thinking person and look around in the API docs.
You can do it with a single method call.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Think like a thinking person and look around in the API docs.
You can do it with a single method call.
no need for this mate, I know you all try to help, I try to do so too if I can, but sometimes you people ( prefesional programers) forget that there are people which just learning, following lecture notes or a book. Going around java api docs and looking for functions in early stage is imposible. You don't know what you looking for, you don't know where to start. Can you remember your self in early stages, learning any language
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
yes, I can remember that. And I'm still constantly learning after programming (first as a student and now professionally) for almost 20 years now.
I was, and still am, constantly with my head down in books and websites/docsets (and previously magazines, before they all devolved into nothing more than paid advertising).
That's why I told you to look in the documentation. The answer is there, and in a rather obvious place too.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
@anishpandey just summarize what you just did Posted in 4 years old thread
Ignored original request (question was string reverse letters, not sentence reverse)
Ignored use of tags on the forum
Double posted same poor formatted code with no comments or suggestions at all
Result, thread closed.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902