Commit 980de3b

mo <mo.khan@gmail.com>
2019-06-03 03:07:14
print 2d array
1 parent 3c1efc3
src/Q1/ReversedSentence.java
@@ -13,7 +13,13 @@ public class ReversedSentence {
   }
 
   public static String printChar2DArray(char[][] arr) {
-    return "";
+    String[] strings = new String[arr.length];
+
+    for (int i = 0; i < arr.length; i++) {
+      System.out.println(arr[i]);
+      strings[i] = new String(arr[i]);
+    }
+    return String.join(System.lineSeparator(), strings);
   }
 
   public static String reverseByCharacter(String s) {
src/Q1/ReversedSentenceTest.java
@@ -35,4 +35,20 @@ public class ReversedSentenceTest extends TestCase {
     assertEquals(
         "mary zad azlittze lazb", ReversedSentence.change5thPosition("mary had a little lamb"));
   }
+
+  public void testPrintChar2DArray() {
+    char[][] matrix = new char[3][80];
+    String sentence = "mary had a little lamb";
+    matrix[0] = ReversedSentence.reverseByCharacter(sentence).toCharArray();
+    matrix[1] = ReversedSentence.reverseByWord(sentence).toCharArray();
+    matrix[2] = ReversedSentence.change5thPosition(sentence).toCharArray();
+
+    String expected =
+        String.join(
+            System.lineSeparator(),
+            new String(matrix[0]),
+            new String(matrix[1]),
+            new String(matrix[2]));
+    assertEquals(expected, ReversedSentence.printChar2DArray(matrix));
+  }
 }