Answers for "how to reverse the string in java"

3

reverse string java

String string="whatever";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println(reverse);
Posted by: Guest on October-14-2020
2

java reverse a string

// Library approach
public static String solution(String str) {
	// StringBuilder is not thread-safe, use StringBuffer
	return new StringBuffer(str).reverse().toString();
}

// DIY approach
public static String solution(String str) {
	char[] chars = str.toCharArray();
	for(int i = 0, j = str.length() - 1; i < j; i++, j--) {
		char ch = chars[i];
		chars[i] = chars[j];
		chars[j] = ch;
	}
	return new String(chars);
}
Posted by: Guest on March-02-2021
7

string reverse in java

String str = "Hello";
String reverse(String str){
  StringBuilder sb = new StringBuilder();
  sb.append(str);
  sb.reverse();
  return sb.toString();
}
Posted by: Guest on May-09-2020
2

Reverse a string in java without using reverse function

// Reverse a string in java without using reverse function
import java.util.Scanner;
public class ReverseWithoutFunction 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string: ");
      String strInput = sc.nextLine();
      int len = strInput.length();
      String strReverse = "";
      System.out.println("Reverse a string without using reverse function: ");
      for(int a = len - 1; a >= 0; a--)
      {
         strReverse = strReverse + strInput.charAt(a);
      }
      System.out.println(strReverse);
      sc.close();
   }
}
Posted by: Guest on February-19-2021
2

Java reverse string array

// reverse string array in java using collections
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ReverseStringArrayUsingCollections
{
   public static void main(String[] args)
   {
      // creating list of strings
      List<String> li = new ArrayList<String>();
      li.add("java");
      li.add("core");
      li.add("world");
      li.add("hello");
      System.out.println("Given list: " + li);
      Collections.reverse(li);
      System.out.println("After using collections: " + li);
   }
}
Posted by: Guest on December-10-2020

Code answers related to "how to reverse the string in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language