Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
November 4, 2019

How to Reverse a String in Java





In this blog, we will learn how to reverse a string in Java. This will be asked in the most of the interviews. This can be implemented in four different ways listed below:

  1. Convert String into Character Array and use For loop
  2. For loop with CharAt() method
  3. Using StringBuffer class
  4. Using StringBuilder class



Method 1:

Convert String into Character Array and use For loop

In the below snippet, we have done the following:

  • Creating a new string and assigning a custom string value to it
  • Converting this string to character array using toCharArray() method
  • Calculating the length of character array and assigning it to a int variable.
  • Using for loop, we will iterate this to print the string in reverse

public class StringReverseDemo {

	public static void main(String[] args) {

		String inputstring = "Krishna";
		char[] chars = inputstring.toCharArray();
		int length = chars.length;
		
		for (int i = length-1; i >=0; i--) {
			System.out.print(chars[i]);
			
		}

	}

}

Method 2:

Convert String into Character Array and use For loop

In the below snippet, we have done the following:

  • Creating a new string and assigning a custom string value to it
  • Converting this string to character array using toCharArray() method
  • Calculating the length of character array and assigning it to a int variable.
  • Creating an another new string to concat the values to a brand new string
  • Using charAt() method, we will iterate this for loop to print the string in reverse and concat it to the newly created string
public class StringReverseDemo {

	public static void main(String[] args) {
		
		String inputstring = "Krishna";
		String revword = "";
		char[] chars = inputstring.toCharArray();
		int length = chars.length;
		
		for (int i = length-1; i >=0; i--) {
			revword = revword+inputstring.charAt(i);
		}
		System.out.println(revword);

	}

}




Method 3:

Using StringBuffer class

We can use StringBuffer class to reverse a string value with help of its inbuilt methods directly as below:


public class StringReverseDemo {

	public static void main(String[] args) {
		
		String inputstring = "Krishna";

		StringBuffer stringbuffer = new StringBuffer(inputstring);
		System.out.println(stringbuffer.reverse());
		
	}

}

Method 4:

Using StringBuilder class

We can use StringBuilder class to reverse a string value with help of its inbuilt methods directly as below:


public class StringReverseDemo {

	public static void main(String[] args) {
		
		String inputstring = "Krishna";

		StringBuilder stringbuilder = new StringBuilder(inputstring);
		System.out.println(stringbuilder.reverse());
		
	}

}



Please watch the Youtube video for better understanding.



Share this post: on Twitter on Facebook on Google+

Write Data in Excel Using Column Number in C# How to Perform Sum of the Digits in a Given Number in Java

Related Posts

How To SWAP Two Numbers in Java Without using Temp Variable

Java IQs

How to SWAP Two Numbers in Java Without Temp variable or Without Third variable

How to Swap Two Numbers in Java with Temp Variable

Java IQs

How to SWAP Two Numbers in Java using Temp Variable

How to Read Properties file in Java

Java IQs, Uncategorized

How to Read Data From Properties File in Java

Compare two arrays in java with out inbuilt functions

Java IQs

How to Compare Two Arrays in Java without built-in functions

Compare two arrays in java

Java IQs

How to Compare Two Arrays in Java using built-in functions

Remove White Spaces From A String in Java

Java IQs

How to Remove Whitespaces from a String in Java

Sum of The digits in a number in Java

Java IQs

How to Perform Sum of the Digits in a Given Number in Java

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023