An MBA in finance imparts and improves management aptitude, inventive ability, critical thinking ability, and so forth. It offers a real-time experience that fabricates a staunch career foundation for students and working professionals. It helps them to thoroughly understand the financial sector.
In Java, you can reverse a string by converting it into a StringBuilder object, then using the
reverse() method to reverse the characters in the string. Here's an example code snippet that demonstrates how to reverse a string in Java:
public class ReverseString {
public static void main(String[] args) {
String originalString = "hello world";
StringBuilder reversedString = new StringBuilder(originalString).reverse();
System.out.println("Original string: " + originalString);
System.out.println("Reversed string: " + reversedString);
}
}
In this example, we first create a string called originalString that we want to reverse. We then create a
StringBuilder object and pass the original string as a parameter to the constructor.
Next, we call the reverse() method on the StringBuilder object to reverse the characters in the string. The reversed string is then stored in the
reversedString variable.
Finally, we print out both the original string and the reversed string using the
println() method.
The code given below demonstrates how to reverse a string.
class reverse
{
public static void main(String args[])
{
String word = "Welcome to MindStick Forum."; // Declare and initialise a string
int n = word.length(); // Store length of the string
String rev = "";
for(int i = n-1; i >= 0; i--)
{
rev += word.charAt(i); // Stores the array in reverse order
}
System.out.println("The reverse is " + rev);
}
}
OUTPUT:
The reverse is .muroF kcitSdniM ot emocleW
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println('Enter any string for reverse: ');
String str = sc.nextLine();
System.out.println(reverse(str));
}
public static String reverse(String in) {
if (in == null)
throw new IllegalArgumentException('Null is not valid input');
StringBuilder out = new StringBuilder();
char[] chars = in.toCharArray();
for (int i = chars.length - 1; i >= 0; i--)
out.append(chars[i]);
return out.toString();
}
}
Output.
Enter any string for reverse: Here are mindstick software pvt ltd.
.dtl tvp erawtfos kcitsdnim era ereH
Liked By
Write Answer
How to reverse a String in Java?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
28-Apr-2023In Java, you can reverse a string by converting it into a StringBuilder object, then using the reverse() method to reverse the characters in the string. Here's an example code snippet that demonstrates how to reverse a string in Java:
In this example, we first create a string called originalString that we want to reverse. We then create a StringBuilder object and pass the original string as a parameter to the constructor.
Next, we call the reverse() method on the StringBuilder object to reverse the characters in the string. The reversed string is then stored in the reversedString variable.
Finally, we print out both the original string and the reversed string using the println() method.
Krishnapriya Rajeev
24-Mar-2023The code given below demonstrates how to reverse a string.
Mukul Goenka
16-Nov-2021Output.
Enter any string for reverse: Here are mindstick software pvt ltd.
.dtl tvp erawtfos kcitsdnim era ereH