简体   繁体   中英

How does recursive method to count white spaces in a string work?

I'm trying to fully understand how the method works, see the code below:

public static void main(String[] args) {
    System.out.println(countspaces("a number of spaces "));
}

public static int countspaces(String s) {
    if (s.length() == 0)
        return 0;
    else
        return (s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1));
}

I've debugged the method using BlueJ. The line:

return (s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1));

firstly checks if the character at the index zero is a white space, then it calls itself again (this makes it recursive) taking the substring of s starting at index 1 as an argument effectively changing the argument from "a number of spaces " to " number of spaces " and doing it till the argument's length() reaches 0. What I don't get is why it's not returning 01000000100100000010 (the last 0 being for the empty string s which terminates the loop) but 4? I can't see where in the code it sums up the 1's returned by

(s.charAt(0) == ' ' ? 1 : 0)

and ignoring the 0's. Please advise me what is missing from my reasoning.

Many Thanks

Grzegorz(Greg)

(s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1))

This one basically sums up the 0 s and 1 s.

Take note of the return value of the method which is an int . The return value of 4 is perfectly fine.

To put in other words:

0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 = 4

Since the method returns an int, not a string, it adds the numbers, not concatenates as characters/strings. ie

0+1+0+0+0+0+0+0+1+0+0+1+0+0+0+0+0+0+1+0 == 4

not

"0"+"1"+"0"+"0"+"0"+"0"+"0"+"0"+"1"+"0"+"0"+"1"+"0"+"0"+"0"+"0"+"0"+"0"+"1"+"0" 
== "01000000100100000010"

below returns an int, since countspaces returns an int

return (s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1));

Here's an attempt to write the function in "English" in case that helps you understand it:

int countspaces( string ) {

    if string has no characters {
        return 0
    }
    else {

        if first character in string is a space 

        add 1 

        otherwise

        add 0

        to result of countspaces( string_with_first_character_removed )
    }
}

The point of recursion is that the function is called over and over again, you could roughly say it's some sort of loop.

if (s.length() == 0)
    return 0;

This code is stopping condition (because the recursion stops at this point) of your recursive function, it will return 0 when the length of provided string is 0. Nothing much to explain here, I think that is pretty obvious.

And this part is core part of your recursive function:

return (s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1));

s.charAt(0) == ' ' ? 1 : 0 s.charAt(0) == ' ' ? 1 : 0 uses a ternary operator , it checks if the first character in string is space, if it's true it uses value of 1, else it uses 0.

countspaces(s.substring(1)) calls the function again, with substring that removes first character.

Function returns int value of 0 or 1, it sums the values returned, and as you know, x + 0 = x , so only the cases where first character is space (function returns 1) affect your final result.

This calls occurs until the function passes itself empty string, when it hits the stopping condition , where it goes back through the call stack, summing returned values and values from ternary operator and finally returns the expected result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM