-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseTheString.java
42 lines (33 loc) · 1.14 KB
/
ReverseTheString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Import modules.
import java.util.Scanner;
/** Copyright (c) 2022 Mel Aguoth All rights reserved.
* This program reverses a string using recursion.
*
* @author Mel Aguoth
* @version 11.0.13
* @since 2022-01-14
*/
class ReverseTheString {
public static String recurseString(String normalString) {
// If the string is empty, return it.
if (normalString.isEmpty()) {
return normalString;
// If the string isn't empty, recurse it with it's first character at the end.
} else {
return recurseString(normalString.substring(1)) + normalString.charAt(0);
}
}
public static void main(String[] args) {
// Introduce the program.
System.out.print("This program reverses a given string using recursion.");
// Get the user's string.
Scanner input = new Scanner(System.in);
System.out.print("\n" + "Enter your string: ");
String userString = input.next();
input.close();
// Call recurseString.
final String reversedString = recurseString(userString);
// Display the reversed string.
System.out.print("\n" + "The reverse of '" + userString + "' is: " + reversedString + "\n");
}
}