-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPractice.java
More file actions
315 lines (268 loc) · 6.83 KB
/
Copy pathStringPractice.java
File metadata and controls
315 lines (268 loc) · 6.83 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
public class StringPractice {
/* returns true if c is a punctuation mark or false otherwise
*
* Punctuation mark is defined as:
* apostrophe '
* comma ,
* period .
* semicolon ;
* colon :
* exclamation point !
* question mark ?
*
* (You don't have to worry about any others)
*/
public static boolean isPunct(char c) {
char apost = '\'';
char comma = ',';
char period = '.';
char semi = ';';
char colon = ':';
char exclPoint = '!';
char queMark = '?';
if(c != apost && c != comma && c != period && c != semi && c != colon && c != exclPoint && c != queMark)
{
return false;
}
return true;
}
/*
* returns the number of punctuation marks
* found in s.
*
* call your isPunct( ) method. Do not copy and paste
* the same logic into this function */
public static int numPunct(String s) {
int count = 0;
for(int i = 0; i < s.length(); i++)
{
if(isPunct(s.charAt(i)) == true)
count ++;
}
if(count > 0)
{
return count;
}
return 0;
}
/*
* returns the number of punctuation marks
* found in s starting at the given index.
*
* call your isPunct( ) method. Do not copy and paste
* the same logic into this function */
public static int numPunct(String s, int startIndex) {
int count = 0;
String newString;
try
{
newString = s.substring(startIndex);
} catch(IndexOutOfBoundsException e)
{
return 0;
}
char[] array = newString.toCharArray();
for(int i = 0; i < array.length; i++)
{
if(isPunct(array[i]))
count ++;
}
return count;
}
/*
* returns the index of the first occurrence
* of a punctuation mark in s starting
* from index startPosition or -1 if there are
* none at index startPosition or later.
*
* When implementing this function, call your isPunct( ) method.
* Do not simply copy and paste the body of isPunct( ) into this method.
*/
public static int indexOfFirstPunct(String s, int startPosition) {
int count = 0;
String newString = s.substring(startPosition);
char[] array = newString.toCharArray();
for(int i = array.length - 1; i >= 0; i--)
{
if(isPunct(array[i]))
count = i;
}
if(count > 0)
return count;
else
return -1;
}
/*
* returns the index of the first punctuation mark in s or
* -1 if s contains no punctuation marks
*
* use your solution to indexOfFirstPunct(String s, int startPosition)
* in this function. Do not repeat the same logic.
*
* Notice that this method has the same name as the
* previous one, but that it takes a different number of arguments. This is
* perfectly legal in Java. It's called "method overloading"
*
*/
public static int indexOfFirstPunct(String s) {
return 0;
}
/*
* returns the index of the last occurrence of a punctuation
* mark in s or -1 if s contains no punctuation
*
* When implementing this function, call your isPunct( ) method.
* Do not simply copy and paste the body of isPunct( ) into this method.
*/
public static int indexOfLastPunct(String s) {
char[] array = s.toCharArray();
for(int i = array.length - 1; i >= 0; i--)
{
if(isPunct(array[i]))
return i;
}
return -1;
}
/*
* returns a new String which is the same as s but with
* each instance of oldChar replaced with newChar
*/
public static String substitute(String s, char oldChar, char newChar) {
// Error, char newString and null.
char[] array = s.toCharArray();
char[] newString = null;
for(int i = 0; i < array.length; i++)
{
if(array[i] == oldChar)
{
array[i] = newChar;
}
newString[i] = array[i];
}
return "";
}
/*
* returns a new String which is the same as s, but
* with each instance of a punctuation mark replaced
* with a single space character
*
* Use at least one of your other functions in your
* solution to this.
*
*/
public static String substitutePunct(String s) {
//Error, null and newString
char[] array = s.toCharArray();
char[] newString = new char[array.length];
for(int i = 0; i < array.length; i++)
{
if(isPunct(array[i]))
array[i] = ' ';
newString[i] += array[i];
}
return newString.toString();
// return "";
}
/*
* returns a new String which is the same as s,
* but with all of the punctuation
* marks removed.
*
* Use at least one of your other functions
* in your solution to this one.
*
*/
public static String withoutPunct(String s) {
//Error, cannot return type;
char[] array = s.toCharArray();
StringBuilder newString = new StringBuilder();
for(int i = 0; i < array.length; i++)
{
if(!isPunct(array[i]))
{
newString.append(array[i]);
}
//newString[i] = array[i];
}
return newString.toString();
//return "";
}
/* returns true if c is found in s or false otherwise */
public static boolean foundIn(String s, char c) {
char[] array = s.toCharArray();
for(int i = 0; i < array.length; i++)
{
if(array[i] == c)
return true;
}
return false;
}
/*
* Returns true of s contains none of the characters
* found in chars or false otherwise.
*/
public static boolean containsNone(String s, String chars) {
char[] array = s.toCharArray();
char[] arrayChars = chars.toCharArray();
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < arrayChars.length; j++)
{
if(array[i] == arrayChars[j])
return false;
}
}
return true;
}
/*
* Returns true of s is comprised of only punctuation or
* false otherwise
*
* Use at least one of your other
* functions in this one.
*/
public static boolean onlyPunct(String s) {
char[] array = s.toCharArray();
int count = 0;
for(int i = 0; i < array.length; i++)
{
if(isPunct(array[i]) == false)
{
count ++;
}
}
if(count > 0)
return false;
return true;
}
/*
* Returns true of s contains no punctuation or
* false otherwise
*
* Use at least one of your other
* functions in this one.
*/
public static boolean noPunct(String s) {
char[] array = s.toCharArray();
for(int i = 0; i < array.length; i++)
if(isPunct(array[i]))
return false;
return true;
}
/*
* returns true if s has two punctuation marks
* right next to each other or false otherwise
*
* use at least one of your other methods
* in your solution to this method
*/
public static boolean consecutivePuncts(String s) {
// Error, i goes out of bounds
char[] array = s.toCharArray();
int first = 0;
for(int i = 1; i <= array.length; i++)
if(isPunct(array[i]) == isPunct(array[first]))
return true;
return false;
}
}