Problem Definition:- Write a Java code to reverse any string. String should be input from the user at the runtime. Write different class for the business logic of reversing a string.

P.S. Here, Eclipse Ganymede is used for coding. And, Exception Handling and entering false strings are not covered this source code. This source code is meant for beginners of Java.


Step 1: Class names :

  • - This class contains main() method from where method of reversing a string, which is defined in another class, is called.
  • - This class contains reverseString() method which contains business logic for reversing a string and its display.

Step 2: Variable declarations in each class :

  • In, following variables are required.
  1. readString with data type as String and initialised to null .

String readString = null;

  • In, following are the variables which will be used.

1. lengthOfStringToReverse with data type as int, initialised to 0 (zero). Purpose of this variable is to store length of string.

int lengthOfStringToReverse = 0;

2. counter with data type as int, initialised to zero. It is just a counter variable to iterate through for loop.

int counter = 0;

Step 3: Now, the actual code



1.  In above source code, there is one class named MainClass. In java, name of the class and name of the file must be same.

2. Inside MainClass, object named "reverse" is created and invoked using new keyword. This object is created of class ReverseStringLogic class , for calling reverseString() method from the               ReverseStringLogic class.

3. In java, to read any text entered during run time, an object of Scanner class needs to be used. Here, scanLine is an object of Scanner class.

4. System.out.println() is used to display text , which will make it easier for any layman user to enter any appropriate text. Here, System is a java class that provides access to the system, out is an output stream that is connected to console, and println() is a method which displays any text that is written inside it, on console.

5. Using, input that is entered during run time, is captured and stored in appropriate variable. Here, that variable is readString.

6. Using object of ReverseStringLogic class, "reverse", method reverseString() is called. This method takes argument as readString of type String. Here, string entered during run time is                 stored in readString variable and is passed through argument in method reverseString().



1. In above snapshot, ReverseStringLogic class is shown which contains reverseString() method, which contains one argument, stringToReverse of type String. As length() method is used for                  variables of data type String, we need to import package java.lang.String. Here , java.lang is a package, and String is a class.

2. length() method is used to calculate  length of any string and length of string is stored in variable lengthOfStringToReverse, which is used in "for" loop while iteration.

3. "for" loop is used to iterate through the string with its variable as counter initialised to length of string i.e. lengthOfStringToReverse minus 1. "Minus 1" is done because in java, if string                    contains 8 characters then the range of storing these characters is from 0 to 7. Condition given as "counter >=0", as we have to reverse the string and finally, we have post decremented the                  counter by 1 "counter--".

4. Inside "for" loop, charAt() method is used, which contains one argument of type integer. charAt() method takes the character present at particular location. In above code, if value of                      counter variable is 5 then, it will take character present at 5th location from original string and is displayed using System.out.println(). The above for loop will run those many times                           depending on length of the string i.e. if length of string is 8 then for loop will run 8 times. Following is the ouput of the above source code.


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

Which online video on demand sites/apps do you think has exceeded your expectations in terms of entertainment vale and variety?

No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 5 answer(s).
[{"id":"23234","title":"Amazon Prime","votes":"10","type":"x","order":"1","pct":45.45,"resources":[]},{"id":"23235","title":"Netflix","votes":"11","type":"x","order":"2","pct":50,"resources":[]},{"id":"23236","title":"Voot","votes":"0","type":"x","order":"3","pct":0,"resources":[]},{"id":"23237","title":"AltBalaji","votes":"1","type":"x","order":"4","pct":4.55,"resources":[]},{"id":"23238","title":"Viu","votes":"0","type":"x","order":"5","pct":0,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.
Why to choose Java? In the computer market, being able to use Java is a crucial knowledge. Java is a development terminology used to build compelling
DATABASE CONNECTIVITY This articles explains the need of database connectivity, the way it is achieved through Java using the JDBC(java database connectivity)
  Write your first simple java program.   First of all we need is a placewhere we can write your Java code. All Java programs are written using
Array of objects in Java Arrays are defined as block of continous memory locations which are arranged in one after another. Elements of the Array can
Synchronized block is alternative mechanism for "synchronized" methods.If we inherihate the non synchronized blocks either from base class or interface