How do i write a word backwards?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 25
Reputation: peedi is an unknown quantity at this point 
Solved Threads: 0
peedi peedi is offline Offline
Light Poster

How do i write a word backwards?

 
0
  #1
Mar 4th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 17
Reputation: bebe11bebe is an unknown quantity at this point 
Solved Threads: 0
bebe11bebe bebe11bebe is offline Offline
Newbie Poster

Re: How do i write a word backwards?

 
0
  #2
Mar 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 25
Reputation: peedi is an unknown quantity at this point 
Solved Threads: 0
peedi peedi is offline Offline
Light Poster

Re: How do i write a word backwards?

 
0
  #3
Mar 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 17
Reputation: bebe11bebe is an unknown quantity at this point 
Solved Threads: 0
bebe11bebe bebe11bebe is offline Offline
Newbie Poster

Re: How do i write a word backwards?

 
0
  #4
Mar 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 25
Reputation: peedi is an unknown quantity at this point 
Solved Threads: 0
peedi peedi is offline Offline
Light Poster

Re: How do i write a word backwards?

 
0
  #5
Mar 4th, 2009
I want the user to input a word, example happy
and i want the program to output on the screen, yppah
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 17
Reputation: bebe11bebe is an unknown quantity at this point 
Solved Threads: 0
bebe11bebe bebe11bebe is offline Offline
Newbie Poster

Re: How do i write a word backwards?

 
0
  #6
Mar 4th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: chili5 is an unknown quantity at this point 
Solved Threads: 2
chili5's Avatar
chili5 chili5 is offline Offline
Newbie Poster

Re: How do i write a word backwards?

 
0
  #7
Mar 4th, 2009
Either use a loop like others are saying or you can take the lazy way out:

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 25
Reputation: peedi is an unknown quantity at this point 
Solved Threads: 0
peedi peedi is offline Offline
Light Poster

Re: How do i write a word backwards?

 
0
  #8
Mar 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 25
Reputation: curtissumpter is an unknown quantity at this point 
Solved Threads: 0
curtissumpter curtissumpter is offline Offline
Light Poster

Re: How do i write a word backwards?

 
0
  #9
Mar 5th, 2009
Whatsup,

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

Here's the reversal code. Later.

-- LiveWire

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: How do i write a word backwards?

 
0
  #10
Mar 5th, 2009
@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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC