Search Bar

Reverse String in core JAVA using string builder and loops

 

How to reverse a String in Java using String Builder and Loops.

 

Hello programmers,
In this tutorial we are going to learn very basic interview codding question that most of the time interviewer are asking for Java Interviews to Reverse a given string in java or Reverse a string in java or java program to reverse a string or Write a java program to reverse a string.

 

Reverse a string in java
Reverse a string in java


1. Using String Builder, Write a java program to reverse a string:

public class ReverseStringExample {
    public static void main(String[] args) {
        String original = "Hello World";
        StringBuilder reversed = new StringBuilder();
        reversed.append(original);
        reversed = reversed.reverse();
        String result = reversed.toString();
        System.out.println(result);
    }
}
 


------------------------------------------------------

2. Using Loop, Reverse a string in Java:

 public class ReverseStringExample {
    public static void main(String[] args) {
        String original = "Hello World";
        String reversed = "";
        for (int i = original.length() - 1; i >= 0; i--) {
            reversed += original.charAt(i);
        }
        System.out.println(reversed);
    }
}


------------------------------------------------------

 Let's go over the details of both programs.

1. Making use of the StringBuilder to reverse a words in a string JAVA.

In this method, the string is inverted using the 'StringBuilder' class.


  • - First, the string variable "original" is declared and set to "Hello World."
  • - Next, a new instance of "StringBuilder" with the name "reversed" is created.
  • - The 'append()' technique is used to add the 'original' string to the reversed StringBuilder.
  • - The reverse ()' method for the reversed StringBuilder is then called. This procedure reverses the characters in the 'StringBuilder' internal buffer.
  • - The inverted "StringBuilder" is then transformed back into a string using the "toString()" method and saved in the string variable "result."
  • - The inverted string "dlroW olleH" is produced when we print the string "result."



2. Making use of a for loop:

This method reverses the original string by looping through the characters in reverse order and constructing a new string.


  • - First, we declare the string variable "original" and set its initial value to "Hello World."
  • - Next, we declare the empty string variable'reversed'.
  • - We iterate through the characters in the string "original" backward using a "for" loop. The index is decreased starting with the last character ("original.length() - 1") and continues until the initial character ("0") is reached.
  • - The character at the current index is obtained inside the loop using the 'charAt()' method, which is then concatenated with the reversed string.
  • - The inverted string 'dlroW olleH' is produced when we print the 'inverted' string at the end.


The string is reversed in both strategies, but they employ different methods to get there. Because of its performance benefits, the "StringBuilder" approach is generally more effective and is advised for string inversion in Java.



The 'StringBuilder' method is as follows:

Since the 'append()' method in 'StringBuilder' just adds the characters to the internal buffer, its temporal complexity is O(1).

The'reverse()' method in 'StringBuilder' has an O(n) time complexity, where n is the length of the string. To reverse the string, it iteratively swaps the characters in the internal buffer.

The 'StringBuilder' technique has an overall time complexity of O(n), where n is the length of the string.

Due to the 'StringBuilder' creating a new string of length n to store the reversed string, the space complexity is also O(n).


Regarding the "for" loop strategy:

The "for" loop has an O(n) time complexity, where n is the length of the string.

Since a new string of length n is constructed to store the reversed string, the space complexity is also O(n).

The time and space complexity of both methods is O(n), where n is the length of the string.

 

 

Post a Comment

0 Comments