943,525 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8398
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 4th, 2009
0

How do i write a word backwards?

Expand Post »
Hello im kinda of new to java and i am stuck on a program. I want to take a word as input and display it in reverse.

ex - hello to olleh

I tried to do the For loop but i am kinda of stuck. Can anyone please help me! thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
peedi is offline Offline
25 posts
since Mar 2009
Mar 4th, 2009
0

Re: How do i write a word backwards?

do a While loop, where you say, while the word is not olleh, do... then you make a loop in which you rearrange the letters one by one, starting from the last one.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bebe11bebe is offline Offline
17 posts
since Feb 2009
Mar 4th, 2009
0

Re: How do i write a word backwards?

isn't a for loop easier? how do i make it to be the reverse of the user input?

import java.util.Scanner;


public class Backwardsword
{

public static void main(String[] args)
{
String word="";
int j;
char[] chr;


System.out.println("Enter a Word:");
Scanner keyboard = new Scanner(System.in);
word= keyboard.nextLine();

chr= word.toCharArray();


for (j=word.length()-1; j>=0; j--)

System.out.println("The word in reverse is " + (chr[j]) + ".");
}
}

I ran my program but the output is

the word in reverse is o.
the word in reverse is l.
the word in reverse is l.
the word in reverse is e.
the word in reverse is h.
Last edited by peedi; Mar 4th, 2009 at 3:04 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
peedi is offline Offline
25 posts
since Mar 2009
Mar 4th, 2009
0

Re: How do i write a word backwards?

well what you got is the word in reverse. is your question why the output is 5 different lines instead of one line saying olleh? if so, look at your System.out.println and you should see it. otherwise, i'm not sure what your question is, because you got the expected output.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bebe11bebe is offline Offline
17 posts
since Feb 2009
Mar 4th, 2009
0

Re: How do i write a word backwards?

I want the user to input a word, example happy
and i want the program to output on the screen, yppah
Reputation Points: 10
Solved Threads: 0
Light Poster
peedi is offline Offline
25 posts
since Mar 2009
Mar 4th, 2009
0

Re: How do i write a word backwards?

your problem is in the last for loop, because you are telling the program to output it character by character by putting the system.out.println each time. you need one system.out.println which will output the entire word
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bebe11bebe is offline Offline
17 posts
since Feb 2009
Mar 4th, 2009
0

Re: How do i write a word backwards?

Either use a loop like others are saying or you can take the lazy way out:

Java Syntax (Toggle Plain Text)
  1. String sText = "bracket";
  2. String sReverse = new StringBuilder(sText).reverse().toString();
  3. System.out.println(sReverse);

A StringBuilder contains a reverse method. Then you reverse it and convert the StringBuilder back to a string with the toString() method.
Reputation Points: 12
Solved Threads: 2
Newbie Poster
chili5 is offline Offline
21 posts
since Dec 2008
Mar 4th, 2009
0

Re: How do i write a word backwards?

i slightly modified my program and came out with this

import java.util.Scanner;
public class Backwardsword
{
public static void main(String[] args)
{
String word="";
System.out.println("Enter a Word:");
Scanner keyboard = new Scanner(System.in);
word= keyboard.next();

for (int j=word.length(); j >=0; j--)
{
System.out.println(word.substring(j-1, j));
}
}
}

I have one error. i run the program and it says index out of range.i dunno what do do! help!
Last edited by peedi; Mar 4th, 2009 at 11:27 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
peedi is offline Offline
25 posts
since Mar 2009
Mar 5th, 2009
0

Re: How do i write a word backwards?

Whatsup,

I'm sure at this point you know how to read the input from the keyboard.

Here's the reversal code. Later.

-- LiveWire

java Syntax (Toggle Plain Text)
  1.  
  2. public class StringReversal
  3. {
  4.  
  5. public static void main(String args[])
  6. {
  7. String str= "", str2 = "you're the man";
  8.  
  9. char ch[] = str2.toCharArray();
  10.  
  11.  
  12. for(int i = ch.length-1; i > -1; i--)
  13. {
  14. str += "" + ch[i];
  15. }
  16.  
  17. System.out.println(str);
  18. System.out.println("Program over");
  19.  
  20. }
  21.  
  22.  
  23.  
  24. }
Reputation Points: 10
Solved Threads: 0
Light Poster
curtissumpter is offline Offline
25 posts
since Nov 2008
Mar 5th, 2009
0

Re: How do i write a word backwards?

@curtissumpter: what do you think you are doing giving people ready made code, this forum boasts an attitude opposite of that. Tell the OP the logic mentioning the way to go about, or at the most the psuedocode, but not written code in any manner.

@peedi : Use a stack to reverse the word and I am sure it will be the most convenient approach. Just keep pushing all the letters till the end of the word and then pop them back one by one.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: switch off the client monitor display
Next Thread in Java Forum Timeline: How to count paragraph for an article?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC