Answers for "sololearn Reverse a String java"

14

how to reverse a string in java

public class ReverseString {
    public static void main(String[] args) {
        String s1 = "neelendra";
        for(int i=s1.length()-1;i>=0;i--)
            {
                System.out.print(s1.charAt(i));
            }
    }
}
Posted by: Guest on March-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
0

reverse string in java

String s = "abcde";
s.reverse();
System.out.println(s);

//s == "edcba"
Posted by: Guest on July-18-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language