Reverse a String

In this article, we are going to discuss how we can reverse a string using code. for this tutorial, we are going to use java for reversing a string.

in java, we can reverse a string in many ways by using the inbuilt function, by converting the string to a char array, adding each character in reverse order, etc.

let’s see how we can reverse a string by accessing each character or array one by 1.

how to reverse a string in java ?

  • take a string input
  • take an empty String variable to store the reverse of the input string.
  • run a loop from i =0 to i = lenght of input string -1;
  • add 1 character in front of the reverse variable by using charAt(i).
  • end of loop;
  • print reverse string variable.

Java code to reverse a string

package gangforcode;

import java.util.Scanner;

public class reverseString {
    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        String input =;
        String reverse="";
        for(int i=0;i<input.length();i++)
            reverse= input.charAt(i)+reverse;

output of

Leave a Comment