Math puzzle with solution

How to reverse string in java using recursion

How to reverse string in java using recursion

how to reverse string in java using recursion learn java code easy method
The reverse string in java using recursion is here

Copy Code Example

Java Code reverse string using recursion


public class ReverseString {
    public static void main(String[] args) {
        String original = "Hello, World!";
        String reversed = reverseRecursively(original);

        System.out.println("Reversed string: " + reversed);
    }

    public static String reverseRecursively(String str) {
        if (str.isEmpty()) {
            return str;
        }
        return reverseRecursively(str.substring(1)) + str.charAt(0);
    }
}


Let's break down the code (reverse string in java) step by step:

1. Class Declaration:

```java

public class ReverseString {

```

- This line declares a public class named `ReverseString`. In Java, every piece of executable code must be within a class.

2. Main Method

```java

    public static void main(String[] args) {

```

- The `main` method is the entry point of any Java application. When you run the program, this method is called first.

- `public` means the method is accessible from anywhere.

- `static` means the method belongs to the class rather than instances of it.

- `void` means the method doesn't return any value.

- `String[] args` is used to pass command-line arguments to the program (not used in this example).

3. Original String:

```java

        String original = "Hello, World!";

```

- This line creates a `String` variable named `original` and assigns it the value `"Hello, World!"`.

4. Reversing the String Recursively:

```java

        String reversed = reverseRecursively(original);

```

- This line calls the `reverseRecursively` method, passing the `original` string as an argument.

- The `reverseRecursively` method processes the string and returns its reversed version, which is then stored in the `reversed` variable.

5. Printing the Reversed String:

```java

        System.out.println("Reversed string: " + reversed);

```

- This line prints the reversed string to the console.

6. Recursive Method to Reverse the String:

```java

    public static String reverseRecursively(String str) {

```

- This method is where the actual string reversal happens.

- `public static` makes the method accessible from anywhere and callable without creating an instance of the class.

- `String` indicates that this method returns a `String` value.

- `reverseRecursively(String str)` is the method signature, where `str` is the string to be reversed.

7. Base Case for Recursion:

```java

        if (str.isEmpty()) {

            return str;

        }

```

- This checks if the input string `str` is empty.

- If it is empty, the method returns the string as is, which effectively ends the recursion.

8. Recursive Case:

```java

        return reverseRecursively(str.substring(1)) + str.charAt(0);

```

- If the string is not empty, the method does two things:

  1. Recursive Call: It calls itself with the substring that excludes the first character: `str.substring(1)`.

  2. Appending First Character: It appends the first character `str.charAt(0)` to the result of the recursive call.

- Example Walkthrough:

  - If `str` is `"Hello"`, `str.substring(1)` gives `"ello"`, and `str.charAt(0)` is `'H'`.

  - The method keeps calling itself with smaller substrings until it reaches an empty string.

  - Once it hits the base case, it starts returning and concatenating characters in reverse order.

9. Final Result:

- The recursion unfolds as follows:

  - `reverseRecursively("Hello")` → `"olleH"`

  - The final result is `"olleH"`, which is returned to the `main` method and printed.

Output:

- The program outputs: `Reversed string: !dlroW ,olleH`

This method of reversing a string using recursion leverages the call stack to process the string in reverse order, building up the reversed string as the recursion unwinds.

Post a Comment

0 Comments