Trying to count blank spaces in a string recursive

2019-09-25 03:50发布

问题:

EDIT: Really sorry, I mean Java! As for what I think, I would say the first contains if statement is for s == null or length 0, but I'm confused as to what to put in the

return spaceCount(s.substring(1, ......)) + ......;

part.

I'm trying to use some if statements to write a function that takes a string as a parameter and recursively coutns the number of blanks spaces " " it has. So far I have

public static int spaceCount (string s) {
    if ( ...... ) {
        return 0;
    }
    char c = s.charAt(0);
    if (....... ) {
        return spaceCount (.....);
    } else {
        return spaceCount(s.substring(1, ......)) + ......;
    }
}

So in the first if statement, should I write the case of the string having zero length? I'm pretty sure that won't cover the case of no spaces at all, so I'm not sure how to proceed.

For the second and third, I know I have to scan the string for spaces, but I am not really sure how to do that either. Any hints or direction would be appreciated!

回答1:

public static int spaceCount(final String s) {

    if(s == null || s.length() == 0) {
        return 0;
    }

    char c = s.charAt(0);
    if(' ' != c) {
        return spaceCount(s.substring(1));
    } else {
        return spaceCount(s.substring(1)) + 1;
    }

}

You don't have to "scan the string for spaces", that's what the recursion passing the remainder of the string does.



回答2:

s.length() - s.replaceAll(" ", "").length() returns you number of spaces.

how to count the spaces in a java string? has the answer. Probably it may help. the above line is the simplest.



回答3:

[You didn't specify a programming language] Here is a solution in Java:

public static int spaceCount(String s)
{ return scRecursive (s, s.length, 0, 0); }

public static int scRecursive (String s, int len, int dex, int count)
{ if (len == dex) return count;
  else
    return scRecursive (s, len, dex + 1,
                        (' ' == s.charAt(dex) ? count + 1 : count)); }

This is tail recursive (which might imply some efficiency) and, more importantly, this does not copy/allocate substrings

Here is one in Scheme:

(define (space-count string)
  (let ((length (string-length string)))
    (let stepping ((index 0) (count 0)
      (if (= index length)
          count
          (let ((char (string-ref string index)))
            (stepping (+ index 1)
                      (if (equal? #\space char)
                          (+ 1 count)
                          count)))))))

The recursion is in the call to stepping which has two arguments - the current index and the current count of spaces. The recursion terminates when the index equals the length. The count is incremented when the current char is a space.



回答4:

public class CountSpaces {

    public static void main(String[] args) {
        String str = "     A   ";
        System.out.println(spaceCount(str, 0));
        System.out.println(spaceCount(str));
    }

    public static int spaceCount(String str, int count) {
        if (str == null) {
            return 0;
        } else if (str.length() > 0) {
            char c = str.charAt(0);
            if (Character.isWhitespace(c)) {
                count++;
            }
            return spaceCount(str.substring(1), count);
        } else {
            return count;
        }
    }

    public static int spaceCount(String s) {
        if (s.length() == 0 || s == null) {
            return 0;
        }
        char c = s.charAt(0);
        if (!Character.isWhitespace(c)) {
            return spaceCount(s.substring(1));
        } else {
            return spaceCount(s.substring(1, s.length())) + 1;
        }
    }
}