| Write code to reverse a string in Java?
Below is the code to reverse string in Java below code will helps you on that:-
public class StringReverseClass
{
public static void main(String args[])
{
String str = "Crackyourinterview";
String reverse = new StringBuffer(str).reverse().toString();
System.out.printf("Word Enter: %s, Word after Reverse %s", str, reverse);
}
public static String reverse(String source)
{
if (source == null || source.isEmpty())
{
return source;
}
String reverse = "";
for (int i = source.length() - 1; i >= 0; i--)
{
reverse = reverse + source.charAt(i);
}
return reverse;
}
}
Output:-
Word Enter: Crackyourinterview, Word after Reverse weivretniruoykcarC
| | |