4
\$\begingroup\$

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
\$\endgroup\$
  • 4
    \$\begingroup\$ What characters can be in the string? Just printable ASCII, or can there be unicode characters, tabs, newlines etc? \$\endgroup\$ – Jo King Sep 19 '19 at 5:56
  • \$\begingroup\$ What is considered a space? Only 32@ASCII or other whitespace characters too? \$\endgroup\$ – Grzegorz Oledzki Sep 19 '19 at 8:21
  • \$\begingroup\$ @JoKing Just printable ASCII if you want. \$\endgroup\$ – HighlyRadioactive Sep 19 '19 at 11:01
  • 3
    \$\begingroup\$ I don't think having one of the parameters hard-coded changes the challenge enough to not make it a dupe \$\endgroup\$ – Jo King Sep 19 '19 at 11:30
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – HighlyRadioactive Sep 19 '19 at 14:08

19 Answers 19

6
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Cool. Is the idea of a prototypical element Dyalog specific? \$\endgroup\$ – Jonah Sep 19 '19 at 5:41
  • 2
    \$\begingroup\$ @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') \$\endgroup\$ – Adám Sep 19 '19 at 5:49
  • \$\begingroup\$ Interesting. Golang has the idea of a type's "zero value", but I've never come across anything analogous in J. \$\endgroup\$ – Jonah Sep 19 '19 at 5:53
  • \$\begingroup\$ @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 \$\endgroup\$ – Adám Sep 19 '19 at 6:01
  • \$\begingroup\$ ah right! that’s where i’d seen it in J! thanks @Adám \$\endgroup\$ – Jonah Sep 19 '19 at 13:37
4
\$\begingroup\$

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
\$\endgroup\$
  • 3
    \$\begingroup\$ The irony of using whitespaces to find whitespaces! \$\endgroup\$ – Night2 Sep 19 '19 at 10:13
  • 1
    \$\begingroup\$ Using Whitespace for this challenge is like using Unreadable for Tex Quotes. \$\endgroup\$ – HighlyRadioactive Sep 19 '19 at 11:11
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ @Night2 Oh, thanks! I didn't notice that. \$\endgroup\$ – Bismarck71 Sep 19 '19 at 8:04
2
\$\begingroup\$

J, 8 bytes

1#.' '=]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 26 bytes

<?=count_chars($argn)[32];

Try it online!

\$\endgroup\$
  • \$\begingroup\$ That was quick. What, there's a built-in in PHP called "count_chars"? Wow! \$\endgroup\$ – HighlyRadioactive Sep 19 '19 at 5:35
  • 1
    \$\begingroup\$ @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! \$\endgroup\$ – Night2 Sep 19 '19 at 5:38
  • 1
    \$\begingroup\$ I will be surprised if this isn't a duplicate... too. But I searched a bit and didn't find anything. \$\endgroup\$ – HighlyRadioactive Sep 19 '19 at 5:39
1
\$\begingroup\$

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

I really hope this is not optimal

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 7 bytes

$_=y; ;

Try it online!

\$\endgroup\$
1
\$\begingroup\$

CJam (5 bytes)

q' e=

Online demo

qS/,(

Online demo

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 18 bytes

($args-eq32).count

Try it online.

Expects input via splatting.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

Jelly, 2 bytes

ċ⁶

Try it online!

Literally ċ count spaces.

\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 6 4 bytes

-2 byte thanks to ngn

+/^:

Try it online!

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

Python 3, 21 bytes

lambda s:s.count(' ')

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 24 22 bytes

-2 bytes thanks to Shaggy and template literals!

s=>s.split` `.length-1

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 2 bytes

Yawn!

èS

Try it

\$\endgroup\$
0
\$\begingroup\$

Ruby, 15 bytes

->s{s.count' '}

Try it online!

Uhmmm...

\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica), 9 bytes

Count@" "

Try it online!

Takes a list of characters as input.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.