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

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support