Practice Task 4: Implement a recursive function to reverse a string. For example, given the input "hello", the function should return "olleh"
Youtube Link: https://youtu.be/FYO0omZbFFw
This project demonstrates a recursive function that takes a string as input and returns the reversed version of that string.
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
The reverseString()
function uses recursion to reverse the input string. Here's how it works:
- If the input string is empty, the function simply returns the empty string.
- Otherwise, the function recursively calls itself with the substring of the input string starting from the second character (i.e.,
input.substring(1)
). - 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.
Here's an example of a GitHub README for this project:
Reverse a string using recursion in Java
This project demonstrates a recursive function that takes a string as input and returns the reversed version of that string.
- Recursive implementation of string reversal
- Easy-to-understand code with comments
- Efficient and elegant solution
- 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)
- Compile and run the
StringReversal
class:cd string-reversal javac StringReversal.java java StringReversal
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.
This project is licensed under the MIT License.
- 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.