How many spaces are there?

5

1

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

HighlyRadioactive

Posted 2019-09-19T05:16:42.660

Reputation: 1 585

Question was closed 2019-09-19T09:23:03.390

4What characters can be in the string? Just printable ASCII, or can there be unicode characters, tabs, newlines etc? – Jo King – 2019-09-19T05:56:26.210

What is considered a space? Only 32@ASCII or other whitespace characters too? – Grzegorz Oledzki – 2019-09-19T08:21:54.917

@JoKing Just printable ASCII if you want. – HighlyRadioactive – 2019-09-19T11:01:49.190

@JoKing It is probably not a duplicate since - You don't have to input the character you need to find, which allows for cool solutions for example: Retina, 1 byte .(Not created for it though) – HighlyRadioactive – 2019-09-19T11:03:48.410

3I don't think having one of the parameters hard-coded changes the challenge enough to not make it a dupe – Jo King – 2019-09-19T11:30:57.857

1@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. – HighlyRadioactive – 2019-09-19T14:08:08.853

@JoKing I won't argue, though. This is my first closed challenge! – HighlyRadioactive – 2019-09-19T14:08:37.360

Answers

6

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.

Adám

Posted 2019-09-19T05:16:42.660

Reputation: 37 779

1Cool. Is the idea of a prototypical element Dyalog specific? – Jonah – 2019-09-19T05:41:15.160

2@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') – Adám – 2019-09-19T05:49:14.400

Interesting. Golang has the idea of a type's "zero value", but I've never come across anything analogous in J. – Jonah – 2019-09-19T05:53:39.707

@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 – 2019-09-19T06:01:26.147

ah right! that’s where i’d seen it in J! thanks @Adám – Jonah – 2019-09-19T13:37:24.413

4

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

Kevin Cruijssen

Posted 2019-09-19T05:16:42.660

Reputation: 67 575

3The irony of using whitespaces to find whitespaces! – Night2 – 2019-09-19T10:13:59.447

1Using Whitespace for this challenge is like using Unreadable for Tex Quotes. – HighlyRadioactive – 2019-09-19T11:11:54.560

3

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.

Bismarck71

Posted 2019-09-19T05:16:42.660

Reputation: 41

@Night2 Oh, thanks! I didn't notice that. – Bismarck71 – 2019-09-19T08:04:18.447

2

J, 8 bytes

1#.' '=]

Try it online!

Jonah

Posted 2019-09-19T05:16:42.660

Reputation: 8 729

1

PHP, 26 bytes

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

Try it online!

Night2

Posted 2019-09-19T05:16:42.660

Reputation: 5 484

That was quick. What, there's a built-in in PHP called "count_chars"? Wow! – HighlyRadioactive – 2019-09-19T05:35:27.170

1

@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!

– Night2 – 2019-09-19T05:38:09.023

1I will be surprised if this isn't a duplicate... too. But I searched a bit and didn't find anything. – HighlyRadioactive – 2019-09-19T05:39:13.567

1

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

I really hope this is not optimal

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

Try it online!

my pronoun is monicareinstate

Posted 2019-09-19T05:16:42.660

Reputation: 3 111

1

K (ngn/k), 6 4 bytes

-2 byte thanks to ngn

+/^:

Try it online!

Galen Ivanov

Posted 2019-09-19T05:16:42.660

Reputation: 13 815

1" " -> 32­­ – ngn – 2019-10-10T13:44:23.900

@ngn Thank you! – Galen Ivanov – 2019-10-10T14:10:04.147

1in k4 monadic ^ compares with null, which is " " for chars, so the solution could be +/^: – ngn – 2019-10-12T08:18:46.490

@ngn Great, thanks! – Galen Ivanov – 2019-10-12T13:53:18.133

1

Jelly, 2 bytes

ċ⁶

Try it online!

Literally ċ count spaces.

Nick Kennedy

Posted 2019-09-19T05:16:42.660

Reputation: 11 829

1

Perl 5, 7 bytes

$_=y; ;

Try it online!

Nahuel Fouilleul

Posted 2019-09-19T05:16:42.660

Reputation: 5 582

1

PowerShell, 18 bytes

($args-eq32).count

Try it online.

Expects input via splatting.

Andrei Odegov

Posted 2019-09-19T05:16:42.660

Reputation: 939

1

CJam (5 bytes)

q' e=

Online demo

qS/,(

Online demo

Peter Taylor

Posted 2019-09-19T05:16:42.660

Reputation: 41 901

1

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.

Kevin Cruijssen

Posted 2019-09-19T05:16:42.660

Reputation: 67 575

1

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.

Unrelated String

Posted 2019-09-19T05:16:42.660

Reputation: 5 300

0

JavaScript, 24 22 bytes

-2 bytes thanks to Shaggy and template literals!

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

Try it online!

Night2

Posted 2019-09-19T05:16:42.660

Reputation: 5 484

0

Python 3, 21 bytes

lambda s:s.count(' ')

Try it online!

Jitse

Posted 2019-09-19T05:16:42.660

Reputation: 3 566

0

Japt, 2 bytes

Yawn!

èS

Try it

Shaggy

Posted 2019-09-19T05:16:42.660

Reputation: 24 623

0

Ruby, 15 bytes

->s{s.count' '}

Try it online!

Uhmmm...

G B

Posted 2019-09-19T05:16:42.660

Reputation: 11 099

0

Wolfram Language (Mathematica), 9 bytes

Count@" "

Try it online!

Takes a list of characters as input.

attinat

Posted 2019-09-19T05:16:42.660

Reputation: 3 495

0

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.

Kevin Cruijssen

Posted 2019-09-19T05:16:42.660

Reputation: 67 575