-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAcronym.java
More file actions
38 lines (29 loc) · 922 Bytes
/
Acronym.java
File metadata and controls
38 lines (29 loc) · 922 Bytes
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
/**
* Convert a phrase to its acronym.
*
* Techies love their TLA (Three Letter Acronyms)!
*
* Help generate some jargon by writing a program that converts a long name like
* Portable Network Graphics to its acronym (PNG).
*/
class Acronym {
String phrase;
Acronym(String phrase) {
this.phrase = phrase;
}
String get() {
String acronym = "";
char[] arrChar = new char[phrase.length()];
for (int i = 0; i < phrase.length(); i++) {
arrChar[i] = phrase.charAt(i);
}
acronym += arrChar[0];
for(int i = 1; i<phrase.length();i++){
if((phrase.substring(i-1, i).equals(" ")&&!phrase.substring(i).equals("_"))||(phrase.substring(i-1, i).equals("-")&&!phrase.substring(i).equals(" "))
||(phrase.substring(i-1, i).equals("-")&&!phrase.substring(i).equals(" "))){
acronym += Character.toUpperCase(phrase.charAt(i));
}
}
return acronym;
}
}