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
Deem
  • 11
  • 1

5 Answers5

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);
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • **Please don't teach string concatenation like this**. They might continue to use it. Use `StringBuilder`. – tilpner Dec 20 '13 at 16:29
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);
}
Konstantin
  • 3,254
  • 15
  • 20
  • **Please don't teach string concatenation like this**. They might continue to use it. Use `StringBuilder`. – tilpner Dec 20 '13 at 16:30
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();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
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();
Kloe2378231
  • 1,404
  • 11
  • 12
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));
Mohammad Jafar Mashhadi
  • 4,102
  • 3
  • 29
  • 49