string reverse in java

Java is one of the most popular programming language used today in developing real world applications (specifically in dynamic and sophisticated web Applications development). Java is strongly typed,object oriented, and multi-platform(even platform independent in general) programming languages. Learning curve with Java is very steep, in beginning ,but it’s very easy when you go ahead, so just relax and move on, solve some problems and write code, That’s the only way you can learn any programming language.

String is considered as an object data type in java. String is immutable (it means you can create and destroy strings but it’s content(characters) can’t can not be altered ) in Java. In this post you will learn, how to reverse a given string in Java. There are lot of ways to reverse a string in Java – the simplest and easy way is to use library methods/functions; but if you are a beginner then better try to do it in different ways because in that way you will learn more.

How to Reverse a String in Java without Using StringBuffer

Here is a program(save it as StringReverse.java and compiler=>execute) to get the reverse string – The code is very simple and comments are written, so no explanation is required,if you have any problem or question or you have any better solution then reply through comment.

import java.util.*;

public class StringReverse {
public static void main(String []args) {
//Get the string to be reversed
System.out.println("Enter the String to reverse");
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
// Test  System.out.println("The Given String was :"+str);
int stringLength = str.length();
// Test  System.out.println("The Length of the String is : "+stringLength);
char rawData[] =new char[50];
for(int k=0;k<50;k++)
rawData[k]=0;
// Using a for loop to iterate over the characters in string
for(int i=0,j=stringLength-1;i<stringLength;i++,j--) {
rawData[i]=str.charAt(j);
}
String reversedString=new String(rawData);
reversedString=reversedString.substring(0,stringLength);
System.out.println("The Reversed String is : "+reversedString);
//int sLength = reversedString.length();
//System.out.println("The Length of the String is : "+sLength);
}
}

If you are looking for the easiest method to reverse a string in Java, then use StringBuffer class and reverse() method, here is the code for that –

import java.util.*;
public class ReverseTheString {
public static void main(String []args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String str=sc.nextLine();
str = new StringBuffer(str).reverse().toString();
System.out.println("Reversed String : "+str);
}
}

Join the Conversation

3 Comments

  1. Also another, quicker way to do it without using a string buffer:

    private static String reverseString(String s)
    {
    /* temporarily holds reversed string */
    String reverse = “”;
    for(int i = 0; i < s.length(); i++)
           {
    // splits string into individual characters
            char[] tempArr = s.toCharArray();
            // Reads the original string of characters backwards one by one and adds it into "reverse"     //string 
            // Works by taking off of one string back to front and adding it to another front to back, thus //reversing it
    char c = tempArr[(s.length()-1)-i];
    reverse = reverse+c;
           }
    return reverse;
    }

Leave a comment

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