Skip to content

Practice Task 4: Implement a recursive function to reverse a string. For example, given the input "hello", the function should return "olleh"

Notifications You must be signed in to change notification settings

ARIBFIB/Recursive-String-Reversal-in-Java-Practice-Task-Lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 

Repository files navigation

Recursive-String-Reversal-in-Java-Practice-Task-Lab

Practice Task 4: Implement a recursive function to reverse a string. For example, given the input "hello", the function should return "olleh"

String Reversal

How it works (5)

Youtube Link: https://youtu.be/FYO0omZbFFw

Recursive String Reversal in Java

image

This project demonstrates a recursive function that takes a string as input and returns the reversed version of that string.

Example

public class StringReversal {
    public static String reverseString(String input) {
        if (input.isEmpty()) {
            return input;
        }
        return reverseString(input.substring(1)) + input.charAt(0);
    }

    public static void main(String[] args) {
        String input = "hello";
        String reversed = reverseString(input);
        System.out.println("Original string: " + input);
        System.out.println("Reversed string: " + reversed);
    }
}

This will output:

Original string: hello
Reversed string: olleh

Another Output

image

How it Works

The reverseString() function uses recursion to reverse the input string. Here's how it works:

  1. If the input string is empty, the function simply returns the empty string.
  2. Otherwise, the function recursively calls itself with the substring of the input string starting from the second character (i.e., input.substring(1)).
  3. The function then concatenates the result of the recursive call with the first character of the input string (input.charAt(0)).

This process continues until the base case (an empty string) is reached, at which point the function starts to unwind the recursive calls and build the reversed string.

GitHub README

Here's an example of a GitHub README for this project:

String Reversal

Reverse a string using recursion in Java

πŸš€ About the Project

This project demonstrates a recursive function that takes a string as input and returns the reversed version of that string.

πŸ› οΈ Features

  • Recursive implementation of string reversal
  • Easy-to-understand code with comments
  • Efficient and elegant solution

πŸ”§ Installation and Usage

  1. Clone the repository:
    [git clone https://github.com/your-username/string-reversal.git](https://github.com/ARIBFIB/Recursive-String-Reversal-in-Java-Practice-Task-Lab.git)
    
  2. Compile and run the StringReversal class:
    cd string-reversal
    javac StringReversal.java
    java StringReversal
    

🀝 Contributing

Contributions are always welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.

πŸ“„ License

This project is licensed under the MIT License.

πŸ‘¨β€πŸ’» Developer

  • Abdul Rehman Irfan

In this example, the GitHub README includes the following elements:

  • A main image for the String Reversal project
  • A beautiful description of the project
  • A section explaining how the recursive string reversal function works
  • Instructions for installation and usage
  • A contribution section
  • A license section
  • Information about the author(s)

The README also uses various fonts, styles, and icons to enhance the visual appeal and organization of the content.

About

Practice Task 4: Implement a recursive function to reverse a string. For example, given the input "hello", the function should return "olleh"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published