Home / Development / Java / Reverse a String in Java
Reverse a String in Java

Reverse a String in Java

There are many ways of reversing a String in Java for whatever reason you may have. Today, we will look at a few simple ways of reversing a String in Java. For example, the string “Reverse Me” once reversed will be “eM esreveR”. We will start by looking at the most traditional method with the least help from external Java classes.

In the above code, we are basically reading in a String from the user afterwhich we will begin an iteration loop that will build the new reversed String. This is done in the “for” loop by obtaining the characters of the original String individually from the end by using the “charAt” function of the String class and concatenating them to a new String by using the “+” operator.

Another method which is also similar but uses the Java StringBuilder class instead:

Very similar to the previous method, the only difference is that we are using the “append” function of the Java StringBuilder class in the iteration loop instead to concatenate the characters to form the reversed String.

Of course there is still an easier shortcut to do it and that would be to simply use the Java StringBuilder class “reverse” function.

What we have seen above are just a few ways to reverse a String in Java. The point is that there are many possible ways to reverse a String in Java and at the end of the day, it boils down to individual requirements, restrictions and what works best for you.

  • Was this Helpful ?
  • yes   no

About Clinton De Silva

17 comments

  1. String cant reverse in java..if u put a first program the output wont show.

  2. In java the string cant reverse..if u type the first program the output wont show..it means there is not a reverse string..

  3. class StringReverse
    {
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();

    char[] c=s.toCharArray();
    for(int i=c.length;i>=1;i–)
    {
    System.out.print(c[i-1]);
    }
    }
    }

  4. yugandhara ghatage

    i need reverse striing …using iterator also

  5. Another method could also be used is:

    public static void main (String args[])
    {
    String s=”This is a string”;
    char arr[]=s.toCharArray(); // convert a string into character array and save it in a variable

    // check if the string is empty or null
    if(s.isEmpty()|| s ==null)
    {
    System.out.println(“Empty string is not accepted “);
    }
    // print the string in reverse order
    for(int i = arr.length-1; i>=0; i–)
    {

    System.out.print(arr[i]);
    }
    System.out.println(” “);
    }
    }

  6. program simple hona chahiye jisse koi v smajh ske …..so, this is so simple..thanku

  7. Shrikrishna Shivajirao Chate

    Another method is:

    public static void main (String args[])
    {
    String s1=”This is a string”;
    int size = s1.length();
    String reverse = “”;

    // print the string in reverse order
    for(int i = size-1; i>=0; i–)
    {
    reverse = reverse + s1.charAt(i);
    }
    System.out.println(reverse);
    }
    }

  8. Easy Understandable simple .. very good

  9. How would you change the code so that it reverses the order of the string, rather than the words in the string ?

  10. How would you change the code so that it reverses the order of the array rather than the words ?

  11. Try Stack to Reverse a String
    By importing java.util.Stack;

  12. Is it possible without using another String or char[] we can reverse a string in same string ?

  13. Last element in the string is not \0, then why wrote there length() -1 in loop

  14. Is it possible without using another String or char[] we can reverse a string in same string ?

Leave a Reply

Your email address will not be published. Required fields are marked *