-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseCharJavaDoc.java
More file actions
119 lines (101 loc) · 4.65 KB
/
ReverseCharJavaDoc.java
File metadata and controls
119 lines (101 loc) · 4.65 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
*@author Wyatt Bechtle
*@since 30 April 2023
*Program: ReverseChar Class
*
*Algorithm
*
*Step 1) Display program explanation.
*Step 2) Get user input.
*Step 3) Validate the input.
*Step 4) Display reversed string via recursion.
*Step 4) Prompt user to quit or go again.
*/
/**Import scanner class to retrieve input from keyboard. */
import java.util.Scanner;
/**
*This class is used to prompt a user for a string that is 3 to 5 (inclusive) words in
*in length and then recursively reverses the string and display it to the screen.
*/
public class ReverseCharJavaDoc {
/**
*This method is used to prompt a user for a string that is 3 to 5 (inclusive) words in
*in length, validate the length is within range, recursively reverse the string and
*display it to the screen, and prompts user to input "Y" or "N" to iterate again.
*Validates that the user inputs "Y" or "N" before quiting or iterating again.
*@param args is not used within this method.
*/
public static void main(String[] args) {
/**Instantiate scanner object. */
Scanner input = new Scanner(System.in);
/**Declare variables to hold user input to iterate program. */
String choice;
/**Display program explanation. */
System.out.println("\nGreetings...\n" +
"This programs takes a short sentence (3 to 5 words)\n" +
"and displays the sentence in reverse order via recursion.");
do {
/**Prompt the user to enter a sentence. */
System.out.print("\nEnter a short sentence (3 to 5 words): ");
String sentence = input.nextLine();
/**Declare and initialize variable to control input validation loop. */
boolean inValid = true;
/**Loop used to validate user input. */
do {
/**Split sentence into individual words. */
String [] wordStrings = sentence.split(" ");
/**Count words to verify within 3 to 5 words(inclusive). */
int wordCount = wordStrings.length;
/**If within valid range, end validation loop. */
if (wordCount < 6 && wordCount > 2) {
inValid = false;
}
/**Invalid input, display error and prompt user to input again. */
else {
/**Display error and prompt the user to enter a sentence. */
System.out.print("\nERROR: Sentence out of range...");
System.out.print("\nEnter a short sentence (3 to 5 words): ");
sentence = input.nextLine();
}
} while (inValid);
/**Display the reversed sentence recursively. */
System.out.print("\nThe recursively reversed string is: ");
reverseDisplay(sentence);
/**New line. */
System.out.println();
/**Loop is used to get user to input "y" or "Y" to continue or "n" or "N" to exit. */
do {
System.out.print("\nWould you like to continue (Y/N)? ");
choice = input.nextLine();/**Get user input. */
/**Display error if input does not meet prompt expectations. */
if (!(choice.equalsIgnoreCase("Y") ||
choice.equalsIgnoreCase("N"))) {
System.out.println("\nInvalid input...\nPlease input (Y/N)");
}
} while (!(choice.equalsIgnoreCase("Y") ||
choice.equalsIgnoreCase("N"))); /**Iterate while invalid input. */
} while (choice.equalsIgnoreCase("Y"));/**Iterate while choice is "Y/y" */
/**Display good-bye. */
System.out.println("\nGood-Bye...");
}
/**
*This method takes a string value and recursively reverses it and then displays the
*value to the screen.
*@param value String value to be reversed via recursion and displayed to screen.
*/
public static void reverseDisplay(String value) {
/**Base case. */
if (value.length() == 0) {
return;/**Tail recursion. */
}
/**
*Recursive case.
*Display char at max index, recursively call reversDisplay passing the substring
*of the original string minus the char that was displayed in the previous statement.
*/
else {
System.out.print(value.charAt(value.length() - 1));
reverseDisplay(value.substring(0, value.length() - 1));
}
}
}