diff --git a/RemoveInstance.java b/RemoveInstance.java new file mode 100644 index 0000000..846de43 --- /dev/null +++ b/RemoveInstance.java @@ -0,0 +1,73 @@ + +public class RemoveInstance { + + public static void main(String args[]) { + + // Given String + String inputString = "javaprogram"; + System.out.println("Input String is: "+ inputString ); + + // Given Character + char inputChar = 'a'; + + RemoveInstance RmvIns = new RemoveInstance(); + + // ItrString1 is the string that holds the manipulated String achieved through iteration. + String ItrString1 = RmvIns.iterate(inputString, inputChar); + System.out.println("1) Iterate through the String, one character at a time, Output is : " + ItrString1); + + // resultString2 is the string that holds the manipulated String that can be solved this in one line . + String onelineString12 = RmvIns.builtInFusolveInOneLine(inputString, inputChar); + System.out.println("2) Find a method in the String class that can solve this in one line, Output is : " + onelineString12); + + } + + /** Start Method -- 1) Iterate through the String, one character at a time */ + public String iterate(String inputString, Character inputChar) throws IllegalArgumentException { + + // Check if input string is empty or NULL + if (inputString == null || inputString.isEmpty() || inputChar == null) { + throw new IllegalArgumentException(); + } + + // Get the size of the length + int strlen = inputString.length(); + + // Use String Builder to append string + StringBuilder outputStringBuilder = new StringBuilder(); + + // Iterate through the String, one character at a time + for (int i = 0; i < strlen; i++) { + /** + * if the character is not equal to the given character, add that + * character to the ItrString1 + */ + char currentChar = inputString.charAt(i); + if (currentChar != inputChar) { + outputStringBuilder.append(currentChar); + } + } + return outputStringBuilder.toString(); + + } + + // End of the Method - iterationMethod + + /** + * Start Method--2. Find a method in the String class that can solve this in one line + */ + public String builtInFusolveInOneLine(String inputString, Character inputChar) + throws IllegalArgumentException { + // Checking if input string is empty or NULL + if (inputString == null || inputString.isEmpty() || inputChar == null) { + throw new IllegalArgumentException(); + } + // replace method will replace the given character with null + String outputString = inputString.replaceAll(inputChar.toString(), ""); + + return outputString; + + } + // End of the Method - builtInFunction + +} diff --git a/RemoveInstanceOccurance.xml b/RemoveInstanceOccurance.xml new file mode 100644 index 0000000..60e5f5e --- /dev/null +++ b/RemoveInstanceOccurance.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/RemoveInstanceOfCharInString.java b/RemoveInstanceOfCharInString.java new file mode 100644 index 0000000..15a47eb --- /dev/null +++ b/RemoveInstanceOfCharInString.java @@ -0,0 +1,51 @@ +import org.testng.Assert; +import org.testng.annotations.Test; + +public class RemoveInstanceOfCharInString { + + /** + * This test validates the input String and Input character passed to the method + */ + @Test(expectedExceptions=IllegalArgumentException.class) + public void testInvalidInputs(){ + RemoveInstance removeInstanceTest = new RemoveInstance(); + removeInstanceTest.builtInFusolveInOneLine(null, null); + removeInstanceTest.iterate(null, null); + + removeInstanceTest.builtInFusolveInOneLine("", 'a'); + removeInstanceTest.iterate("", ' '); + } + + /** + * This test validates the 1. Iterate through the String, one character at a time condition + */ + @Test + public void testIterativeApproach(){ + RemoveInstance removeOccurences = new RemoveInstance(); + String outputBasic = removeOccurences.iterate("javaprogram", 'a'); + Assert.assertEquals("jvprogrm", outputBasic); + String outputWithNumber = removeOccurences.iterate("javaprogram123123", '1'); + Assert.assertEquals("javaprogram2323", outputWithNumber); + String outputWithSpace = removeOccurences.iterate("java program string", ' '); + Assert.assertEquals("javaprogramstring", outputWithSpace); + String outputWithSpecialChar = removeOccurences.iterate("java-program-string", '-'); + Assert.assertEquals("javaprogramstring", outputWithSpecialChar); + } + + /** + + * This test validates the 2. Find a method in the String class that can solve this in one line condition + + */ + @Test + public void testBuiltInFunction(){ + RemoveInstance removeOccurences = new RemoveInstance(); + String outputBasic = removeOccurences.builtInFusolveInOneLine("javaprogram", 'a'); + Assert.assertEquals("jvprogrm", outputBasic); + String outputWithNumber = removeOccurences.builtInFusolveInOneLine("javaprogram123123", '1'); + Assert.assertEquals("javaprogram2323", outputWithNumber); + String outputWithSpace = removeOccurences.builtInFusolveInOneLine("java program string", ' '); + Assert.assertEquals("javaprogramstring", outputWithSpace); + String outputWithSpecialChar = removeOccurences.builtInFusolveInOneLine("java-program-string", '-'); + Assert.assertEquals("javaprogramstring", outputWithSpecialChar); + } + +}