| | |
How do i write a word backwards?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 25
Reputation:
Solved Threads: 0
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.
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.
Either use a loop like others are saying or you can take the lazy way out:
A StringBuilder contains a reverse method. Then you reverse it and convert the StringBuilder back to a string with the toString() method.
Java Syntax (Toggle Plain Text)
String sText = "bracket"; String sReverse = new StringBuilder(sText).reverse().toString(); 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.
•
•
Join Date: Mar 2009
Posts: 25
Reputation:
Solved Threads: 0
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!
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.
•
•
Join Date: Nov 2008
Posts: 25
Reputation:
Solved Threads: 0
Whatsup,
I'm sure at this point you know how to read the input from the keyboard.
Here's the reversal code. Later.
-- LiveWire
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)
public class StringReversal { public static void main(String args[]) { String str= "", str2 = "you're the man"; char ch[] = str2.toCharArray(); for(int i = ch.length-1; i > -1; i--) { str += "" + ch[i]; } System.out.println(str); System.out.println("Program over"); } }
@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.
@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 !!!
![]() |
Similar Threads
- reverse the order of words in a sentence (Pascal and Delphi)
- I found this article on Tips & Tricks (Windows tips 'n' tweaks)
- Huffman coding Issue (C++)
- delphi swap word (Pascal and Delphi)
- Are these features included in Vista (Windows Vista and Windows 7)
- StringUtil (Java)
- palindrome problem (C)
- Help with Palindrome program... (Assembly)
- error C2375: 'my_strdup' : redefinition; different linkage (C++)
- backward string (C++)
Other Threads in the Java Forum
- Previous Thread: switch off the client monitor display
- Next Thread: How to count paragraph for an article?
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor





