The Artima Developer Community
Sponsored Link

Java Answers Forum
Reversing a String

5 replies on 1 page. Most recent reply: Mar 30, 2004 9:39 PM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
John

Posts: 2
Nickname: won
Registered: Mar, 2002

Reversing a String Posted: Mar 28, 2002 12:10 AM
Reply to this message Reply
Advertisement
Write a program that takes a word from the keyboard, reverses it and prints the reversed word to the monitor.

This is what I need to do...I have done this thus far:
import java.io*
public class Reverse{
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));

public static void main(String args[])throws IOException {

prompt("Please enter a word: ");
String myWord = keyboard.readLine();
display(myWord);
exitProgram();
}

public static String getWord(String myWord)throws IOException
{
int length = myWord.length();
String newWord = "";
if(!myWord.equals(""))
{
for(int i = length - 1; i>=0; i--)
{
newWord += myWord.charAt(i);

}
}

return myWord;}}

I can't get it to work...can anyone help me??? It simply returns the word the user inputs.


Yousuf

Posts: 19
Nickname: saeed
Registered: Mar, 2002

Re: Reversing a String Posted: Mar 28, 2002 3:38 AM
Reply to this message Reply
Hello John..
I have debugged your programe Now its working fine..
If you want you can have input and output in infinite loop ,where you will get output without exiting.

        import java.io.*;
 
	public class Reverse{
	
	public static void main(String args[])throws IOException {
		BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Please enter a word: ");
		String myWord = keyboard.readLine();
		String revStr=reverse(myWord);
		System.out.println(revStr);
		System.exit(0);
	}
		public static String reverse(String myWord)
			{
			int length = myWord.length();
			String newWord = "";
			if(!myWord.equals(""))
			{
			for(int i = length - 1; i>=0; i--)
				{
				newWord += myWord.charAt(i);
				}
			}
			return newWord;
			}
	
}
 

Just copy it and run after compiling..
If you still have problem you can contact me at yousuf@security-park.com
Bye!!!
Yousuf

bob

Posts: 1
Nickname: sillybob
Registered: Mar, 2002

Re: Reversing a String Posted: Mar 28, 2002 8:54 AM
Reply to this message Reply
Why not use the reverse function in StringBuffer class instead?

Jimmy Y

Posts: 2
Nickname: yjimmy
Registered: Apr, 2002

Re: Reversing a String Posted: Apr 2, 2002 11:09 PM
Reply to this message Reply
Bob is right!
Here is the simple code, and it works fine!

import java.io.*;

public class ReverseString {
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter string: ");
StringBuffer sb = new StringBuffer(br.readLine());
System.out.println("Reversed string: " + sb.reverse().toString());
}
}

calvin van

Posts: 3
Nickname: calvinvan
Registered: Mar, 2004

Re: Reversing a String Posted: Mar 30, 2004 7:52 PM
Reply to this message Reply
Hi Jymmy

Can your code takes a sectence and returns the sentence in reverse order
which display like this:

Enter string:
welcome world
Reverse string:
world welcome

If you can, can you show me how to do it? Many thanks

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Reversing a String Posted: Mar 30, 2004 9:39 PM
Reply to this message Reply
the code work correctly,

Enter string:

hello world
Reversed string: dlrow olleh

Flat View: This topic has 5 replies on 1 page
Topic: BufferedImage Docos Previous Topic   Next Topic Topic: need help !

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use