# How many spaces are there? [duplicate]

Your task is to input a string, and output the number of spaces in the string.

This is code-golf, so least number of bytes win. Your

# Test Cases

Double quotes are not part of the test cases.

"test" => 0
"Hello, World!" => 1
"C O D E G O L F" => 7
"      " => 6

• What characters can be in the string? Just printable ASCII, or can there be unicode characters, tabs, newlines etc?
– Jo King
Sep 19 '19 at 5:56
• What is considered a space? Only 32@ASCII or other whitespace characters too? Sep 19 '19 at 8:21
• @JoKing Just printable ASCII if you want.
– null
Sep 19 '19 at 11:01
• I don't think having one of the parameters hard-coded changes the challenge enough to not make it a dupe
– Jo King
Sep 19 '19 at 11:30
• @JoKing But I don't think there's a lot of challenge which have an 1-byte Retina solution. Also, space are special for a lot of languages, see APL answer, 05AB1E answer and so much more.
– null
Sep 19 '19 at 14:08

# APL (Dyalog Extended), 3 bytesSBCS

+/=


Try it online!

+/ sum the mask where = equal to the prototypical element of that element, which is space for characters.

• Cool. Is the idea of a prototypical element Dyalog specific? Sep 19 '19 at 5:41
• @Jonah It is typical (!) of APL, although some new hobby implementations do not implement this. E.g. in APL2, APLX, and Dyalog is ' ' '   '(0 0 ' ') the prototype of 'a' 'abc'(1 2 'c')
Sep 19 '19 at 5:49
• Interesting. Golang has the idea of a type's "zero value", but I've never come across anything analogous in J. Sep 19 '19 at 5:53
• @Jonah While neat, it does cause various issues, like needing to call f each once on empty arrays to determine the prototype and inability to get a prototype from certain elements (e.g. objects), so Iverson and Hui decided J would only do specific prototypes for simple arrays, while all boxes would give a:, so ' '={.0$'hello' and 0={.0$1 2 3 and a:={.0$'hello';1 2 3 – Adám Sep 19 '19 at 6:01 • ah right! that’s where i’d seen it in J! thanks @Adám Sep 19 '19 at 13:37 # Whitespace, 93 bytes [S S S N _Push_0][N S S N _Create_Label_LOOP][S N S _Duplicate_top][S N S _Duplicate_top][T N T S _Read_STDIN_as_character][T T T _Retrieve_input][S S S T S T S N _Push_10][T S S T _Subtract][S N S _Duplicate_top][N T S T N _If_0_jump_to_label_DONE][S S S T S T T S N _Push_22][T S S T _Subtract][N T S S N _If_0_jump_to_label_SPACE][N S N N _Jump_to_Label_LOOP][N S S S N _Create_Label_SPACE][S S S T N _Push_1][T S S S _Add][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_DONE][S N N _Discard_top][T N S T _Print_as_integer]  Letters S (space), T (tab), and N (new-line) added as highlighting only. [..._some_action] added as explanation only. Since Whitespace inputs one character at a time, the input should contain a trailing newline so it knows when to stop reading characters and the input is done. Try it online (with raw spaces, tabs and new-lines only). An appropriate language for the challenge I guess. PS: The program itself contains 44 spaces. Explanation in pseudo-code: Integer counter = 0 Start LOOP: Character c = STDIN as character If(c == ' '): Call function SPACE Else-if(c == '\n'): Call function DONE Go to next iteration of LOOP function SPACE: counter = counter + 1 Go to next iteration of LOOP function DONE: Print counter as integer to STDOUT  • The irony of using whitespaces to find whitespaces! Sep 19 '19 at 10:13 • Using Whitespace for this challenge is like using Unreadable for Tex Quotes. – null Sep 19 '19 at 11:11 # 05AB1E, 3 2 bytes ð¢  Try it online! Simple explanation:  ð Pushes ' ' space character onto stack ¢ Counts the spaces in the input string  Found the 'ð' Command which saves 1 Byte for pushing a space. • @Night2 Oh, thanks! I didn't notice that. Sep 19 '19 at 8:04 # J, 8 bytes 1#.' '=]  Try it online! # PHP, 26 bytes <?=count_chars($argn)[32];


Try it online!

• That was quick. What, there's a built-in in PHP called "count_chars"? Wow!
– null
Sep 19 '19 at 5:35
• @TwilightSparkle Probably the simplest challenge I have ever seen :P I will be surprised if this isn't already a duplicate. substr_count can also be used to count, but it will be 27 bytes: Try it online! Sep 19 '19 at 5:38
• I will be surprised if this isn't a duplicate... too. But I searched a bit and didn't find anything.
– null
Sep 19 '19 at 5:39

# C# (Visual C# Interactive Compiler), 20 bytes

I really hope this is not optimal

a=>a.Count(b=>b==32)


Try it online!

# Perl 5, 7 bytes

$_=y; ;  Try it online! ## CJam (5 bytes) q' e=  Online demo qS/,(  Online demo # PowerShell, 18 bytes ($args-eq32).count


Expects input via splatting.

# Java 8, 27 bytes

s->s.split(" ",-1).length-1


Try it online.

Explanation:

s->                         // Method with String as parameter and integer return-type
s.split(" ",              //  Split the input on spaces
-1)           //  While retaining empty strings as items
.length    //  Get the amount of items in the array
-1  //  And subtract 1


Using the count() builtin with a filter for spaces would be 2 bytes longer (or 1 if the input is guaranteed to not contain tabs/newlines/other characters below spaces - in which case the ==32 can be <33):

s->s.filter(c->c==32).count()


Try it online.

# Brachylog, 5 bytes

{∋Ṣ}ᶜ


Try it online!

{  }ᶜ    The output is the number of ways in which
∋       an element of the input can be chosen
Ṣ      such that the element is a space.


Alternatively,

# Brachylog, 5 bytes

ṇ₁l-₁


Try it online!

  l      The length of
ṇ₁       the input split on spaces
-₁    minus one is the output.


# Jelly, 2 bytes

ċ⁶


Try it online!

Literally ċ count ⁶ spaces.

# K (ngn/k), 6 4 bytes

-2 byte thanks to ngn

+/^:


Try it online!

• " " -> 32­­
– ngn
Oct 10 '19 at 13:44
• @ngn Thank you! Oct 10 '19 at 14:10
• in k4 monadic ^ compares with null, which is " " for chars, so the solution could be +/^:
– ngn
Oct 12 '19 at 8:18
• @ngn Great, thanks! Oct 12 '19 at 13:53

# Python 3, 21 bytes

lambda s:s.count(' ')


Try it online!

# JavaScript, 24 22 bytes

-2 bytes thanks to Shaggy and template literals!

s=>s.split .length-1


Try it online!

# Japt, 2 bytes

Yawn!

èS


Try it

# Ruby, 15 bytes

->s{s.count' '}


Try it online!

Uhmmm...

# Wolfram Language (Mathematica), 9 bytes

Count@" "


Try it online!

Takes a list of characters as input.

# MathGolf, 5 bytes

gÅ =£


Input as a list of characters.

Try it online.

Explanation:

g      # Filter the (implicit) input-list
Å     #  Using the following two commands:
=   #  Equals a space
£  #  Then pop this filtered list and get its length for the amount of spaces
# (after which the entire stack joined together is output implicitly)


Or alternatively: mÅ =Σ (Try it online), which is similar but uses a map & sum instead of filter & length.