code: private static int countSpaces(String str) {
      int counter = 0;
      for (char c : str.toCharArray()) {
        if (c == ' ') {
          counter++;
        }
      }
      return counter;
    }
docs: Counts spaces in the string.
  
  @param str The string.
  @return The number of spaces.