Question

Was given this by my lecturer, and some parts have confused me!

String removeAll(char c, String s) {

String to_return = "";

while(true) {
    if (s.equals(""))
        return to_return;

    // at this point s is not ""
    char c2 = s.charAt(0);
    if (c2 != c)
    {
        to_return += c2;
    }

    s = s.substring(1);
}

return to_return; // won't be reached

}

System.out.println(removeAll('l',"hello"));
  1. I understand the code is to remove all the l from hello, I just don't understand how it does it!

  2. c2 = h (first char of hello), if it is not equal to c to_return += c2? what does this mean, more importantly what is +=

  3. The next step is very confusing for me too! s = s.substring(1); what does this do in this case I only understand substrings by (1,4) etc!

Thanks for helping

Was it helpful?

Solution

c2 = h (first char of hello), if it is not equal to c to_return += c2? what does this mean, more importantly what is +=

Basically, if the current first char of the String is not equal to c (the character to remove), append it to the to_return value. += is a shorthand (in this String concatenation) for to_return = to_return + c

The next step is very confusing for me too! s = s.substring(1); what does this do in this case I only understand substrings by (1,4) etc!

s.substring(1) creates a new String of s starting from the second character, remember, String is, essentially, 0 indexed character array. This is basically trimming of the first character of the s and assigning the result back to s.

So, for example, if s equals Hello, after s = s.substring(1), s will be equal to ello

Think of it like s.substring(1, s.length() - 1). Take a look at String#substring(int) for more details

return to_return; // won't be reached is true, but

if (s.equals(""))
    return to_return;

Ensures that it will, eventually return a value. Personally, I like to have a single entry and exit point for all my methods, as it reduces the complexity of probability that you'll miss something that will have you scratching your head, but that's just me...

The reason that the last return is required is because the compiler can't determine if the method could ever return or not.

OTHER TIPS

char c2 = s.charAt(0);

You are taking the first character of s. In this case, on the first iteration, it will be h.

if (c2 != c)

If c2(h) is not equal to c(l), then

to_return += c2;

this is called shorthand notation. You can think of that as

to_return = to_return + c2;

So, it concatenates c2 to to_return and assigns the result to to_return. So, if initially to_return was "", after this statement it would have become h.

Now that we have considered the first character, we want to eliminate from the further consideration. So,

s = s.substring(1);

This creates a substring, starting from index 1 till the end of the string. So, we excluded the first character. On the next iteration, s.charAt(0) would be e, since h is not there in variable s anymore.

Apart from that, you could have written the same function, like this

String removeAll(char c, String s) {
    String to_return = "", char c2;

    while (s.equals("") == false) {
        c2 = s.charAt(0);
        if (c2 != c) {
            to_return += c2;
        }
        s = s.substring(1);
    }

    return to_return;
}

System.out.println(removeAll('l', "hello"));
   static String removeAll(char c, String s) {

        String to_return = "";

        while(true) {
            if (s.equals(""))
                break;


            char c2 = s.charAt(0); //take first character of string 
            if (c2 != c) //check if char is diffrent, append it to result string else skip that same char using substring(1)
            {
                to_return += c2;
            }

            s = s.substring(1); //skip char if it is found to remove it from final result string. again in next while loop 
                                //do the same process..fetch first chr and compare with our supplied char
        }

        return to_return; // won't be reached

        }

I also added break insted of return as you will get compilation error due to while(true)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top