Reverse String
Hi
I am very new to java and i am trying to reverse a string of text, the coe below works perfectly but i need the text to be input at the command prompt window and then it shows the output fo the reversed text as opposed tothe way i have set the code up below. does that make sense?
I basically need to have a line that says please enter your text to be reversed:
and it then reverses.
Thanks
public class TestStringReverse {
public static void main(String[] args) {
String string = "Enter string to be reversed";
String reverse = "";
int len = string.length();
if(len == 1) {
reverse = string;
} else {
for(int count = string.length()-1; count >= 0; count-- ) {
reverse += string.charAt(count);
}
}
System.out.println(reverse);
[841 byte] By [
ladystara] at [2007-11-27 8:48:38]

If I understand your question correctly, you will need to concatonate "String[] args" into a single string.
How would i do that?, im confusedThanks
If you place quotes " " around the command line input it will be treated as a single arg.
Wait! I'm confused. Can you explain in detail what your program is supposed to do. In particular where you get the String you want reversed.
I need to create a reverse string feature that allows
*The user should be prompted to enter the a string(line of text)
*The feature should then proceed to construct another String object whichcontains the characters from the string entered by the user in reverse order.
?This string should then be displayed to the keyboard.
and this is what i have so far
public class TestStringReverse {
public static void main(String[] args) {
String string = "Enter string to be reversed";
String reverse = "";
int len = string.length();
if(len == 1) {
reverse = string;
} else {
for(int count = string.length()-1; count >= 0; count-- ) {
reverse += string.charAt(count);
}
}
System.out.println(reverse);
Thanks.
Please Try This one.
public class Test {
public static void main(String[] args) {
String string = "Enter string to be reversed";
String userString = "";
String reverse = "";
System.out.println(string+ "\n");
try{
BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
userString = data.readLine();
}catch(IOException e){
}
int len = userString.length();
if(len == 1) {
reverse = string;
} else {
for(int count = userString.length()-1; count >= 0; count-- ) {
reverse += userString.charAt(count);
}
}
System.out.println(reverse);
}
}
U have to study Java.IO package .
Thanks
HiI tried that but then i realised that that was for buffer and i am using scanner class. Thanks anyways
> i am using scanner class.
No you aren't. That is why I asked you to explain how you get your input.
String string = "Enter string to be reversed";
This does not automagically print a message out, get user to enter input and assign it to the variable called string. You get no use input and if you code worked the only thing it would reverse would be Enter string to be reversed.
OnlyForJavaPlease do not supply full coded solutions. We are not impressed by your leet skillz and all you do is help the OP to cheat. They will learn nothing.
I think ur right. Giving complete code is like spoon feedingThanks for ur Suggestion
I think you can learn more on java GUI by rewriting the whole app using Swing for better management (JOptionPane etc). It is more practical to learn GUI especially for standalone java application.
try this
public class ReverseStringTest {
public static void main(String[] args) {
String str = "What's going on?";
System.out.println(ReverseString.reverseIt(str));
}
}
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--)
dest.append(source.charAt(i));
return dest.toString();
}
}
> try this
>
> public class ReverseStringTest {
>public static void main(String[] args) {
>String str = "What's going on?";
>System.out.println(ReverseString.reverseIt(str));
> }
> }
>
> class ReverseString {
>public static String reverseIt(String source) {
>int i, len = source.length();
>StringBuffer dest = new StringBuffer(len);
>for (i = (len - 1); i >= 0; i--)
>dest.append(source.charAt(i));
>return dest.toString();
> }
> }
Wouldn't that return:
?no gniog s'tahW
OK, no one can read anymore. You code to reverse a string is correct, and you don't even have to worry about a length of 1 as a special case, since your loop code will print it correctly anyway.
to read in data from a user,
String prompt = "Enter a string to reverse";
Scanner in = new Scanner(System.in);
System.out.println(prompt);
String input = in.nextLine();
String reverse = "";
for(int i = input.length()-1; i >= 0; i--)
{
reverse += input.charAt(i);
}
System.out.println("Reverse : " + reverse);
~Tim
i had run this in eclipse & got the required result
I dont understand why sooooooo much code is written just to reverse the String.
Just do the following..
String string = "Enter string to be reversed";
StringBuffer buffer = new StringBuffer(string);
buffer.reverse();
String reverse = buffer.toString();
Doesnt it look simple and efficient!!!
Cheers!!
one more thing..DO not use String class for manipulating the string as they are immutable and meant only for readonly purposes.Use StringBuffer or StringBuilder instead.
IMHO, it's perfectly fine to use the String class to manipulate character strings. String is immutable because Java uses pooling, so if for some reason there are several String instances that use the same word (e.g. "Enter", or a username) you only get one object, but all references point to that object. So, if you make the String class mutable you'd get several changes to all instances of that one string.
But StringBuffer and StringBuilder do basically the same thing as String behind the scenes. It's just that each reference gets its own object. They're only noticeably more efficient when you want to construct many, many strings at one time.
> [...] meant only for readonly
> purposes.
Why would they be meant for read-only purposes? You HAVE to write to them at some point. I think you mean "write once, read-only thereafter." But as I said, they're meant to make sharing clean and easy, not to be read-only.
~C. R.
@vinaak_r: Well, thanks for that, but my experience is the most Professors would frown upon using an API function like that when the point of the exercise is to teach loops and such. Most likely, the OP was specifically told not to use .reverse() in her code, and beside the point, she had that part fo the code working to start with and only wanted direction on getting input from the user at the command line.
~Tim
> IMHO, it's perfectly fine to use the String class to
> manipulate character strings.
>
@C.R.Cren
jus go thro this!!
http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html
i m just concerned with the line: reverse+= <.....>
> > [...] meant only for readonly
> > purposes.
ya.. its write once read many
Maybe you should re-read my post. All you did was prove my point. I admitted that String OBJECTS are read-only/immutable. The REFERENCES are NOT nor are they meant to be treated that way. That's why we have operations that DO change and write to them.
And the underlying operations, as you see in the URL you provided, ARE in fact very similar. The only time anyone would notice a difference is when many, many objects are created and manipulated.
String objects are more memory-friendly. StringBuffer is processor-friendly. There's a trade-off just like everything else in computer science.
-C. R.
P.S. - Learn how to spell! What did you want me to "go thro[w]" again?
If you are doing this for a class assignment, then you should know that the classic string reversal problem is done as an introduction to "post order recursion".Your method is iterative, is that really what the instructor asked for?