-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringsProj.java
79 lines (66 loc) · 2.84 KB
/
stringsProj.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
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
// Andrew Sala
// stringsProj.java
public class stringsProj {
private static void miniProj4(String name) { // format name
System.out.println(name); // original input
int comma = name.indexOf(","); // finds where the comma is in the string
String first = name.substring(comma+2); // the FIRST name if everything AFTER the comma
String last = name.substring(0,comma); // the LAST name if everything BEFORE the comma
System.out.println(first + " " + last); // prints each variable after
// easy
}
private static void miniProj5(String s) { // reverse each
System.out.println(s); // original sentance
String[] words = s.split(" "); // splits each word in sentance by the spaces
String reverseString = ""; // blank string that will be appended to
for (String word : words) { // for
String reverseWord = ""; // blank string that will be each word
for (int j = word.length() - 1; j >= 0; j--) {
reverseWord += word.charAt(j); // adding the letter of each word starting from the last letter
}
reverseString += reverseWord + " "; // adds each reverse word to the first blank string
}
System.out.println(reverseString); // sentance with reversed words
// still easy
}
private static void miniProj6 (String phrase){ // how many multiples
System.out.println(phrase); // original phrase
int counter = 0;
String [] array = new String[phrase.length()];
for (int i = 0; i < array.length; i++){
array[i] = "word";
}
for (int i = 0; i < phrase.length(); i++){
String letter = phrase.substring(i, i + 1);
int p = phrase.length() - phrase.replace(letter, "").length();
if (p >= 2){
array[counter] = (phrase.substring(i, i + 1) + " repeats " + (p) + " times");
counter++;
}
}
if (counter == 0){
System.out.println("No repeats");
}
for (int i = 0; i < array.length; i++){
String arrayval = array[i];
for(int j = 0; j < array.length; j++){
if (array[j].equals(arrayval) && j != i){
array[j] = "word1";
}
}
}
for (String k: array){
if (!k.equals("word") && !k.equals("word1")){
System.out.println(k);
}
}
// not easy
}
public static void main(String[] args) {
miniProj4("Bloomberg, Mike");
System.out.println("----------------------------");
miniProj5("Hey guy how ya been");
System.out.println("----------------------------");
miniProj6("hello world");
}
}