浏览代码

added method to create acronym

Margaret Pierse 6 年前
父节点
当前提交
eb1c9956b5
共有 1 个文件被更改,包括 11 次插入1 次删除
  1. 11
    1
      LoopFun.java

+ 11
- 1
LoopFun.java 查看文件

@@ -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
       /**