Reverse of a String

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 60:-  Reverse of a String

To reverse a string in Java, you can use multiple approaches, such as using a StringBuilder, StringBuffer, or manually reversing the string by iterating through it. Here are three different methods to reverse a string:

  1. Using StringBuilder or StringBuffer:

javaCopy code

public class StringReverser {    public static String reverseString(String str) {        StringBuilder reversedString = new StringBuilder(str);        return reversedString.reverse().toString();    }    public static void main(String[] args) {        String originalString = "Hello, World!";        String reversedString = reverseString(originalString);        System.out.println("Original String: " + originalString);        System.out.println("Reversed String: " + reversedString);    } }

Output:

yamlCopy code

Original String: Hello, World! Reversed String: !dlroW ,olleH

  1. Manually reversing the string using a loop:

javaCopy code

public class StringReverser {    public static String reverseString(String str) {        StringBuilder reversedString = new StringBuilder();        for (int i = str.length() - 1; i >= 0; i--) {            reversedString.append(str.charAt(i));        }        return reversedString.toString();    }    public static void main(String[] args) {        String originalString = "Hello, World!";        String reversedString = reverseString(originalString);        System.out.println("Original String: " + originalString);        System.out.println("Reversed String: " + reversedString);    } }

Output:

yamlCopy code

Original String: Hello, World! Reversed String: !dlroW ,olleH

  1. Using Java 8 Streams:

javaCopy code

import java.util.stream.Collectors; public class StringReverser {    public static String reverseString(String str) {        return str.chars()                  .mapToObj(c -> String.valueOf((char) c))                  .collect(Collectors.joining(""))                  .reverse();    }    public static void main(String[] args) {        String originalString = "Hello, World!";        String reversedString = reverseString(originalString);        System.out.println("Original String: " + originalString);        System.out.println("Reversed String: " + reversedString);    } }

Output:

yamlCopy code

Original String: Hello, World! Reversed String: !dlroW ,olleH

All three methods will produce the reversed string, and you can choose the one that suits your preference and requirements. The first two methods use StringBuilder for better performance, while the third method uses Java 8 Streams for a more functional approach.

9. Strings

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?