-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1PT2.java
More file actions
229 lines (196 loc) · 7.07 KB
/
P1PT2.java
File metadata and controls
229 lines (196 loc) · 7.07 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.util.*; // Import the Scanner class
/** A class to alter strings
* @author Jake Model
*/
public class P1PT2 {
public static String inputString;
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("\nWelcome\nEnter string: ");
inputString = scanner.nextLine();
start();
}
// method for start screen
public static void start() {
try {
System.out.println("\nWelcome\n 1. Palindrome Check \n 2. Anagram Check \n 3. Add Substring \n 4. Get Length \n 5. Count Occurences \n 6. Reverse Sentence \n 7. Quit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
switch(choice) {
case 1:
// palindrome check
palindromeRecursive(inputString);
start();
break;
case 2:
// anagram check
anagramChecker(inputString, "");
start();
break;
case 3:
// add substring
addSubstring(inputString, "", -1);
start();
break;
case 4:
// get length
getLength(inputString);
start();
break;
case 5:
// count occurances
occurenceCounter(inputString, "");
start();
break;
case 6:
// reverse sentence
System.out.println("\n" + sentenceReversal(inputString) + "\n");
start();
break;
case 7:
// quit
System.out.println("Have a nice day :)");
break;
default:
// return to start
System.out.println("Invalid choice");
start();
break;
}
}
// if an entry is not valid
catch (Exception e) {
System.out.println("Invalid choice");
start();
}
}
// method to check if it is a palindrome
public static boolean palindromeRecursive(String input) {
input = input.toLowerCase();
int length = input.length() - 1;
StringBuilder input2 = new StringBuilder();
// check string input is not empty
if ((input.isEmpty()) || (length == 1)) {
System.out.println("\ntrue\n");
return true;
}
else {
for (int index = 1; index < length; index++) {
if (input.charAt(index) != ' ') {
input2.append(input.charAt(index));
}
}
}
// check for palindrome
if ((input.isEmpty() != true) && ((input.charAt(0) == input.charAt(length)) && (palindromeRecursive(input2.toString()))))
return true;
else {
System.out.println("\nfalse\n");
return false;
}
}
// return true if a string is an anagram of another
public static boolean anagramChecker(String x, String y) {
System.out.print("\nEnter string to compare: ");
while (y.equals("")) {
y = scanner.nextLine();
}
if (x.length() == y.length()) {
int countx = 0;
int county = 0;
x.toLowerCase();
y.toLowerCase();
// turn strings to sorted arrays
char charArrayX[] = x.toCharArray();
char charArrayY[] = y.toCharArray();
Arrays.sort(charArrayX);
Arrays.sort(charArrayY);
// compare each index of the array
for (int index = 0; index < (charArrayX.length); index++) {
if (charArrayX[index] != charArrayY[index]) {
System.out.println("false");
return false;
}
}
System.out.println("\ntrue\n");
return (countx == county);
}
System.out.println("false");
return false;
}
// return the substring added at a given index
public static String addSubstring(String input, String substring, int index) throws NullPointerException{
System.out.print("\nSubstring to be inserted: ");
boolean checker = true;
// obtain substring & index
while (substring.equals("")) {
substring = scanner.nextLine();
while (!substring.equals("") && (checker)) {
System.out.println("Index placement:");
index = scanner.nextInt();
if (index != -1)
checker = false;
}
if (!substring.equals("") && (index != -1))
break;
}
// ensure valid index
if (index > input.length()) {
System.out.println("\nIndex too large");
throw new NullPointerException();
}
if (index < 0) {
System.out.println("Index too small");
throw new NullPointerException();
}
// insert substring
StringBuilder newString = new StringBuilder();
for (int counter = 0; counter < index; counter++) {
newString.append(input.charAt(counter));
}
for (int counter = 0; counter < substring.length(); counter++) {
newString.append(substring.charAt(counter));
}
for (int counter = (index); counter < input.length(); counter++) {
newString.append(input.charAt(counter));
}
System.out.println("\n" + newString + "\n");
return newString.toString();
}
// return the length of a string
public static int getLength(String input) {
System.out.println("\nLength is " + input.length() + "\n");
return input.length();
}
// return # of occurrences of a substring in the string
public static int occurenceCounter(String input, String substring) {
System.out.print("\nEnter substring: ");
// aquire substring
while (substring.equals("")) {
substring = scanner.nextLine();
}
// split input string to different array inputs
int count = 0;
String[] inputArray = input.split(" ");
// check if the input array value equals substring
for (String x : inputArray) {
if (x.equals(substring)) {
count++;
}
}
System.out.println("\n" + count + "\n");
return count;
}
// return the sentence in a reversed order
public static String sentenceReversal(String input) {
// find spaces
int index = input.indexOf(" ");
// base case
if (index == -1) {
return input;
}
// recursive return call
return
sentenceReversal(input.substring(index + 1)) + " " + input.substring(0, index);
}
}