Browse Source

added method to create acronym

Margaret Pierse 6 years ago
parent
commit
eb1c9956b5
1 changed files with 11 additions and 1 deletions
  1. 11
    1
      LoopFun.java

+ 11
- 1
LoopFun.java View File

@@ -24,7 +24,17 @@ public class LoopFun
24 24
        * @return Upper case string of the first letter of each word
25 25
        */
26 26
       public String acronym(String phrase) {
27
-          return null;
27
+          int firstSpaceIndex = phrase.indexOf(" ");
28
+          int lastSpaceIndex = phrase.lastIndexOf(" ");
29
+          phrase = phrase.toUpperCase();
30
+          String result = phrase.substring(0,1);
31
+          while (lastSpaceIndex > firstSpaceIndex && firstSpaceIndex != -1) {
32
+              result = result + phrase.substring(firstSpaceIndex+1, firstSpaceIndex+2);
33
+              phrase = phrase.substring(0,firstSpaceIndex) + phrase.substring(firstSpaceIndex+1);
34
+              firstSpaceIndex = phrase.indexOf(" ");
35
+              System.out.print(result);
36
+            }
37
+          return result;
28 38
       }
29 39
 
30 40
       /**