given string is \'_home sweet home__\' if user enter the mode as 0 then o/p should be \'home sweet home__\' if user enter the mod
You could try something like this:
/**
* Remove white spaces from string according to mode
*
* @param str string
* @param mode mode 0=leading, 1=trailing, 2=leading+trailing
* @param result - result buffer
* @return number of white spaces removed
*/
public int removeWhiteSpacesByMode(String str, int mode, StringBuilder result) {
int n = 0;
switch(mode) {
case 0:
n = removeLeadingWhiteSpaces(str, result);
break;
case 1:
n = removeTrailingWhiteSpaces(str, result);
break;
case 2:
StringBuilder tmp = new StringBuilder();
n = removeTrailingWhiteSpaces(str, tmp);
n += removeLeadingWhiteSpaces(tmp.toString(), result);
break;
default:
throw new IllegalArgumentException("mode=" + mode);
}
return n;
}
private int removeTrailingWhiteSpaces(String str, StringBuilder result) {
int n = 0;
if(str != null && result != null) {
n = str.length()-1;
while(Character.isWhitespace(str.charAt(n))) {
n--;
}
n++;
for(int j = 0; j < n; j++) {
result.append(str.charAt(j));
}
n = str.length() - n;
}
return n;
}
private int removeLeadingWhiteSpaces(String str, StringBuilder result) {
int n = 0;
if(str != null && result != null) {
while(Character.isWhitespace(str.charAt(n))) {
n++;
}
for(int j = n; j < str.length(); j++) {
result.append(str.charAt(j));
}
}
return n;
}
It makes use a of the method Character#isWhitespace to check whether a character is white space or not and a StringBuilder to build the result. The return value is the number of white paces removed.
If you want to have a method to just count the white spaces in a string you can loop over the entire string, check each character using Character#isWhitespace and increment a variable if it return true.
Finally here is some testing:
@Test
public void removeWhiteSpacesByMode() {
String str = " home sweet home ";
StringBuilder result = null;
int numberOfWhiteSpacesRemoved = 0;
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(str, 0, null);
Assert.assertEquals(numberOfWhiteSpacesRemoved, 0);
result = new StringBuilder();
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(null, 0, result);
Assert.assertEquals(0, result.length());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 0);
try {
result = new StringBuilder();
numberOfWhiteSpacesRemoved = 0;
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(null, 4, result);
Assert.fail("mode 4 should not have been accepted");
} catch(IllegalArgumentException e) {
Assert.assertEquals("mode=4", e.getMessage());
Assert.assertEquals(0, result.length());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 0);
}
result = new StringBuilder();
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(str, 0, result);
Assert.assertEquals("home sweet home ", result.toString());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 1);
result = new StringBuilder();
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(str, 1, result);
Assert.assertEquals(" home sweet home", result.toString());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 2);
result = new StringBuilder();
numberOfWhiteSpacesRemoved = removeWhiteSpacesByMode(str, 2, result);
Assert.assertEquals("home sweet home", result.toString());
Assert.assertEquals(numberOfWhiteSpacesRemoved, 3);
}