xxxxxxxxxx
String reverse = new StringBuilder(string).reverse().toString();
xxxxxxxxxx
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));
}
}
}
xxxxxxxxxx
public class StringReverseExample{
public static void main(String[] args) {
String string = "abcdef";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println("\nString before reverse: "+string);
System.out.println("String after reverse: "+reverse);
}
}
xxxxxxxxxx
public class ReverseString {
//1. using for loop and charAt
public static void main(String[] args)
{
String s="Container";
String reverse="";
for(int i=s.length()-1;i>=0;i--)
{
reverse=reverse+(s.charAt(i));
}
System.out.println(reverse);
}
xxxxxxxxxx
String str = "Hello";
String reverse(String str){
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.reverse();
return sb.toString();
}
xxxxxxxxxx
public static void main(String[] args)
{
String input = "Geeks for Geeks";
StringBuilder input1 = new StringBuilder();
// append a string into StringBuilder input1
input1.append(input);
// reverse StringBuilder input1
input1.reverse();
// print reversed String
System.out.println(input1);
}
xxxxxxxxxx
public class ReverseStringByFavTutor
{
public static void main(String[] args) {
String stringExample = "FavTutor";
System.out.println("Original string: "+stringExample);
// Declaring a StringBuilder and converting string to StringBuilder
StringBuilder reverseString = new StringBuilder(stringExample);
reverseString.reverse(); // Reversing the StringBuilder
// Converting StringBuilder to String
String result = reverseString.toString();
System.out.println("Reversed string: "+result); // Printing the reversed String
}
}
xxxxxxxxxx
// Not the best way i know but i wanted to challenge myself to do it this way so :)
public static String reverse(String str) {
char[] oldCharArray = str.toCharArray();
char[] newCharArray= new char[oldCharArray.length];
int currentChar = oldCharArray.length-1;
for (char c : oldCharArray) {
newCharArray[currentChar] = c;
currentChar--;
}
return new String(newCharArray);
}
xxxxxxxxxx
String reverse(String s) {
if(s.length() == 0)
return "";
return s.charAt(s.length() - 1) + reverse(s.substring(0,s.length()-1));
}
xxxxxxxxxx
1)
String str = "Hello";
String result = "";
for(int i = str.length()-1; i>=0; i--){
result += str.charAt(i); // first solution, charAt method
// result += str1.substring(i, i+1); // first solution, substring method
}
System.out.println(result);
}