diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6391605 --- /dev/null +++ b/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + stringProblem + stringProblem + 0.0.1-SNAPSHOT + + + + junit + junit + 4.12 + + + + \ No newline at end of file diff --git a/src/main/java/com/company/util/MyString.java b/src/main/java/com/company/util/MyString.java new file mode 100644 index 0000000..a2c43a6 --- /dev/null +++ b/src/main/java/com/company/util/MyString.java @@ -0,0 +1,28 @@ +package com.company.util; + +public class MyString { + + /* Given a String and a Character, remove all instances of the Character in the String + * Iterate through the String, one character at a time + * + */ + public String removeCharInString(String sStr, char c) { + StringBuilder st = new StringBuilder(); + for(int i = 0; i < sStr.length(); i++) { + if(sStr.charAt(i) != c) + st.append(sStr.charAt(i)); + } + + return st.toString(); + } + + + /* Given a String and a Character, remove all instances of the Character in the String + * Find a method in the String class that can solve this in one line + * + */ + public String removeCharInStringWithReplace(String sStr, Character c) { + return sStr.replaceAll(c.toString(), ""); + } + +} \ No newline at end of file diff --git a/src/test/java/com/company/test/MyStringTest.java b/src/test/java/com/company/test/MyStringTest.java new file mode 100644 index 0000000..5f6b18b --- /dev/null +++ b/src/test/java/com/company/test/MyStringTest.java @@ -0,0 +1,76 @@ +package com.company.test; + +import org.junit.Assert; + +import org.junit.Test; + +import com.company.util.MyString; + +public class MyStringTest { + + @Test + public void test1() { + String str = "APPLE"; + char c = 'A'; + + Assert.assertEquals("PPLE", new MyString().removeCharInString(str, c)); + } + + @Test + public void test11() { + String str = "It has a application"; + char c = 'a'; + + Assert.assertEquals("It hs PPLICTION", new MyString().removeCharInString(str, c)); + } + + @Test + public void test111() { + String str = "EAGLE"; + char c = 'P'; + + Assert.assertEquals("EAGLE", new MyString().removeCharInString(str, c)); + } + + @Test + public void test1111() { + String str = ""; + char c = Character.MIN_VALUE; + + Assert.assertEquals("", new MyString().removeCharInString(str, c)); + } + + + @Test + public void test2() { + String str = "APPLE"; + char c = 'A'; + + Assert.assertEquals("PPLE", new MyString().removeCharInStringWithReplace(str, c)); + } + + + @Test + public void test22() { + String str = "APPLICATION"; + char c = 'A'; + + Assert.assertEquals("PPLICTION", new MyString().removeCharInStringWithReplace(str, c)); + } + + @Test + public void test222() { + String str = "EAGLE"; + char c = 'P'; + + Assert.assertEquals("EAGLE", new MyString().removeCharInStringWithReplace(str, c)); + } + + @Test + public void test2222() { + String str = ""; + char c = Character.MIN_VALUE; + + Assert.assertEquals("", new MyString().removeCharInStringWithReplace(str, c)); + } +} diff --git a/src/test/java/com/company/test/ParameterizedStringTest.java b/src/test/java/com/company/test/ParameterizedStringTest.java new file mode 100644 index 0000000..024d299 --- /dev/null +++ b/src/test/java/com/company/test/ParameterizedStringTest.java @@ -0,0 +1,68 @@ +package com.company.test; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.ComparisonFailure; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.company.util.MyString; + +/* + * + */ +@RunWith(value = Parameterized.class) +public class ParameterizedStringTest { + + private String psInputString = null; + private Character pReplaceChar; + private String psExceptedOutput = null; + + public ParameterizedStringTest(String sInputString, Character replaceChar, String sExceptedOutput) { + psInputString = sInputString; + pReplaceChar = replaceChar; + psExceptedOutput = sExceptedOutput; + } + + + @Parameters + public static List data() { + // Multiple inputs + Object[][] input = { + {"APPLE", 'A', "PPLE"}, + {"It has a application", 'a', "It hs ppliction"}, + {"APPLICATION", 'a', "APPLICATION"}, + {"my app's", '\'', "my apps"}, + {"It has a application", ' ', "Ithasaapplication"} + }; + + return Arrays.asList(input); + } + + + @Test + public void testRemoveCharInString() { + try { + Assert.assertEquals(psExceptedOutput, new MyString().removeCharInString(psInputString, pReplaceChar)); + System.out.println("Success (testRemoveCharInString) - String input : " + psInputString + ", replace character : " + pReplaceChar); + } + catch(ComparisonFailure exp) { + System.out.println("Failed (testRemoveCharInString) - String input : " + psInputString + ", replace character : " + pReplaceChar); + } + } + + @Test + public void testRemoveCharInStringWithReplace() { + try { + Assert.assertEquals(psExceptedOutput, new MyString().removeCharInStringWithReplace(psInputString, pReplaceChar)); + System.out.println("Success (removeCharInStringWithReplace) - String input : " + psInputString + ", replace character : " + pReplaceChar); + } + catch(ComparisonFailure exp) { + System.out.println("Failed (removeCharInStringWithReplace) - String input : " + psInputString + ", replace character : " + pReplaceChar); + } + } +}