reverse string java
String string="whatever";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println(reverse);
reverse string java
String string="whatever";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println(reverse);
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);
}
string reverse in java
String str = "Hello";
String reverse(String str){
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.reverse();
return sb.toString();
}
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();
}
}
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);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us