Reversing a String In Java

Reversing a String In Java | How to Reverse a String in Java uisng charAt() Method.
Hello Readers...

In this post we are going to learn how to reverse a String in Java using for loop and charAt() method of String class. see example.

public class Reverse_String {

     public static void reverseString(String str) {
           String rev="";
           for(int i=str.length()-1;i>=0;) {
                rev=rev+str.charAt(i);
                i--;
           }
           System.out.print("Reverse of "+str+" is : "+rev);
     }
     public static void main(String[] args) {
          
           reverseString("DCBA");

     }

}

Output
Reverse of DCBA is : ABCD

In the above example i have created a simple class named as Reverse_String with main method. I have created a method named as reverseString() which takes String as an argument and declared a String empty/blank variable rev and performing the task of reversing of given String and print reverse of string. For any query use comment box, thanks.

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database