views:

507

answers:

8

I'm trying to come up with with shortest C function (including spaces, any macros that are substituted into it) possible that is passed a NULL-terminated string and returns the number of spaces in that string. This is what I've got so far:

int n(char*l){return*l?!(*l-32)+n(++l):0;}

Which is 42 characters long. Any improvements?

+7  A: 

38 characters:

n(char*l){return*l?!(*l-32)+n(++l):0;}

In c, the int return type declaration is superfluous.

Ben M
Aren't the semicolon for separating statments, so that you can remove it after the last statement?
Guffa
The semicolon primarily terminates, secondarily separates--so no, you can't remove it.
Ben M
In c89, the `int` type is superfluous, but c99 requires a return type.
Chris Lutz
The OP didn't state a version of C :)
Joey
Nice one, 38 chars! Anything else I could've done more concisely? Or a completely different approach?
David Claridge
`!(*l-32)+n(++l)`: it's not defined whether `l` will be incremented after or before `*l`. Use `!(*l-32)+n(1+l)` instead :)
Johannes Schaub - litb
@litb: not defined? the left side of a binary expression, in this case !(*l-32), is evaluated before the right side.
Ben M
Johannes Schaub - litb
@litb: I read up, and you're right--I stand corrected. By contrast, C# guarantees left-to-right operand evaluation (within the same precedence group): do you know if this is generally the case with other modern languages?
Ben M
A: 
#define meh return*l?!(*l-32)+n(++l):0

int n(char *l){meh;}

18 characters :P

kakon
Most code-golfs include `#define`s and preprocessor directives as part of the solution.
Chris Lutz
I'd call that 38 + 19 = 57 charactersIf you allow #define's to be uncounted, there's nothing stopping you from #defining the entire function.
David Claridge
I wasn't aware that #define constructs were functions; being as the op stated "I'm trying to come up with with shortest C function possible". Apologies and thanks :)
kakon
#defines aren't functions, but their code is essentially directly copy-pasted in place of the macro by the preprocessor before compilation.
Nick Lewis
+1  A: 

40 - unfortunately, using (char*)1 as sentinel

m(l){return l-1?1+n(strchr(l,32)+1):-1;}
Adrian Panasiuk
A nice solution if you're allowed to use library functions. :)
David Claridge
Should the 'n' at 'n(strchr(l,32)+1)' be 'm'?
Jonathan Leffler
lol ur right
Adrian Panasiuk
A: 
computergeek6
+1  A: 

Obviously it's just for fun, so why limit it to C? :-)

APL

∇n x
1⊥x=' '
∇

For everybody who thinks APL is hard to read, note that you can convert this almost directly to longhand (i.e., Python):

def n(x):
    return sum(i==' ' for i in x)

Just say "del" instead of "def" and you're halfway there!

Alec
+1 for APL
I. J. Kennedy
+1  A: 

Haskell:

n=length.filter(==' ')
yairchu
Python. s.count(' ')
David Claridge
A: 

Clojure - 31 chars.

(def c #(count(re-seq #"\s"%)))

Exploded:

(defn count-spaces
  [astr] (count
           (re-seq #"\s" astr)))

count returns the number of items in the sequence, which, in this case, is the number of matches to the regex.

nilamo
A: 

Golfscript - 10 chars

{" "/,(}:f

Here is the equivalent in python (Yes this is obviously not as short as using .count(' '))

f=lambda s:len(s.split(" "))-1

Iterating through the input counting spaces is one byte longer

{{32=},,}:f
gnibbler