Spaces:
Running
Running
Create 21. Clean up string
Browse files
Week 2: Methods, strings and lists/21. Clean up string
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
String clean(String wprd)
|
| 4 |
+
|
| 5 |
+
which returns a string with "all characters except upper and lower case letters and spaces" stripped from the string given as a parameter.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
Example method call:
|
| 9 |
+
public static void main(String[] args){
|
| 10 |
+
String test = "Hel1234!&%lo";
|
| 11 |
+
String cleaned = clean(test);
|
| 12 |
+
System.out.println(cleaned);
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
Program outputs:
|
| 16 |
+
Hello
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
import java.util.Random;
|
| 23 |
+
|
| 24 |
+
public class Test{
|
| 25 |
+
public static void main(String[] args){
|
| 26 |
+
final Random r = new Random();
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
String[] words = {
|
| 30 |
+
"a.b.c.",
|
| 31 |
+
"m1213i342x&#/¤(9985e4456d463?",
|
| 32 |
+
"a1b2c3d4e5f6g7h8i9j10",
|
| 33 |
+
"!he\"#re¤% &/in() t,.,-he.- m&(#)iddle*^** of&% tr&&ash& is¤ %%a% mes%#%sage"
|
| 34 |
+
|
| 35 |
+
};
|
| 36 |
+
for (String w : words) {
|
| 37 |
+
System.out.println("Test with parameter " + w);
|
| 38 |
+
System.out.println("Cleaned: " + clean(w));
|
| 39 |
+
System.out.println("");
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
//ADD
|
| 46 |
+
// public static String clean(String wprd) {
|
| 47 |
+
// String finalWord = "";
|
| 48 |
+
// for (char character: wprd) {
|
| 49 |
+
// // uppercase
|
| 50 |
+
// if (character >= 'A' && character <= 'Z') {
|
| 51 |
+
// finalWord+=character;
|
| 52 |
+
// }
|
| 53 |
+
// //lowercase
|
| 54 |
+
// else if (character >= 'a' && character <= 'z') {
|
| 55 |
+
// finalWord+=character;
|
| 56 |
+
// }
|
| 57 |
+
// //spaces
|
| 58 |
+
// else if (character == ' ') {
|
| 59 |
+
// finalWord+=character;
|
| 60 |
+
// }
|
| 61 |
+
// }
|
| 62 |
+
// return finalWord;
|
| 63 |
+
// }
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
//ADD
|
| 67 |
+
public static String clean(String wprd) {
|
| 68 |
+
String finalWord = "";
|
| 69 |
+
for (int i = 0; i < wprd.length(); i++) {
|
| 70 |
+
char character = wprd.charAt(i);
|
| 71 |
+
// uppercase
|
| 72 |
+
if (character >= 'A' && character <= 'Z') {
|
| 73 |
+
finalWord += character;
|
| 74 |
+
}
|
| 75 |
+
// lowercase
|
| 76 |
+
else if (character >= 'a' && character <= 'z') {
|
| 77 |
+
finalWord += character;
|
| 78 |
+
}
|
| 79 |
+
// space
|
| 80 |
+
else if (character == ' ') {
|
| 81 |
+
finalWord += character;
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
return finalWord;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
Test with parameter a.b.c.
|
| 101 |
+
Cleaned: abc
|
| 102 |
+
|
| 103 |
+
Test with parameter m1213i342x&#/¤(9985e4456d463?
|
| 104 |
+
Cleaned: mixed
|
| 105 |
+
|
| 106 |
+
Test with parameter a1b2c3d4e5f6g7h8i9j10
|
| 107 |
+
Cleaned: abcdefghij
|
| 108 |
+
|
| 109 |
+
Test with parameter !he"#re¤% &/in() t,.,-he.- m&(#)iddle*^** of&% tr&&ash& is¤ %%a% mes%#%sage
|
| 110 |
+
Cleaned: here in the middle of trash is a message
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|