0

The program supposed to ask the user to enter a string then the program will reverse it and display it, but the code only return the first letter

import java.util.*; public class ReverseString { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter a String: "); String s = scan.next(); int x = s.length(); char c = ' '; for(int i=x-1; i>=0;i--){ c = s.charAt(i); } System.out.print("The reverse of String " + s + " is "); System.out.print(c); } } 

Output:

 Enter a String: Welcome The reverse of String Welcome is W 
4
  • You don't ever create a new string (just a single character assignation from last character to first character) Commented Dec 20, 2013 at 14:15
  • 1
    How exactly you're going to store a string within a char? Commented Dec 20, 2013 at 14:15
  • why r u using char it can hold only one char Commented Dec 20, 2013 at 14:19
  • I wasn't going to use char but the teacher used it in an example different from this and then asked to solve this I was wondering how would it work. Thanks for the help all :D ! Commented Dec 20, 2013 at 21:12

5 Answers 5

1

You're overwriting value of c in each iteration, change it to string and add to it in the loop

String c = ""; for(int i=x-1; i>=0;i--){ c += s.charAt(i); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't teach string concatenation like this. They might continue to use it. Use StringBuilder.
1

Change char c = ' '; to String reverse=""; and append the character to it for each iteration.

The problem in your code is char c can hold only one character at a time.

Do like this

 String reverse =""; for(int i=s.length()-1; i>=0;i--){ reverse += s.charAt(i); } System.out.print(reverse); 

1 Comment

Please don't teach string concatenation like this. They might continue to use it. Use StringBuilder.
1

A character is one letter; not a string.... the easiest way to do this, would be to use a StringBuilder like so -

public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter a String: "); String s = scan.next(); // int x = s.length(); // char c = ' '; // for (int i = x - 1; i >= 0; i--) { // c = s.charAt(i); //} StringBuilder c = new StringBuilder(s); c = c.reverse(); System.out.print("The reverse of String " + s + " is "); System.out.print(c); } 

Or, if you want to use your current approach you can (by printing one character at time) like this -

public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter a String: "); String s = scan.next(); System.out.print("The reverse of String " + s + " is "); int x = s.length(); for (int i = x - 1; i >= 0; i--) { char c = s.charAt(i); System.out.print(c); } System.out.println(); } 

Comments

0

If you want to make laugh your teacher (or maybe not) ;)

org.apache.commons.lang.StringUtils.reverse(s) 

or without lib:

new StringBuffer(s).reverse().toString(); 

Comments

0

More efficient approach:

char[] strArray = s.toCharArray(); int len = strArray.length; int max = (int)Math.ceil(len / 2.0); char t; for (int i = 0; i < max; ++i) { t = strArray[i]; strArray[i] = strArray[len - i - 1]; strArray[len - i - 1] = t; } System.out.println(String.valueOf(strArray)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.