# Smallest code to return sum of all characters in an array of strings

Smallest code to return sum of all characters of an array of C-strings, including null-byte-terminattor of each string in the array.

example in C:

    const char *a[] = { "abc", "bdef", "cee", "deee" };
size_t n = sumOfAllBytes(a);
printf("n = %d\n", n); // n = 18


e.g.,

   strlen(a[0]) + 1 + ... + strlen(a[n])


where n is array-size-1, ie, the last elemet in the array, in this case, deee word. Note that n = 4 (starting from 0 as C does)

Each character is an ASCII character in C it's 1 byte-size.

• How do you define the value of each character? – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Mar 28 '14 at 18:11
• Do you have to create a complete program, or is it OK to write only a function with an array as parameter? – ProgramFOX Mar 28 '14 at 18:41
• How do you know how many strings are in the array? Your example doesn't have any sort of length argument or sentinel value to indicate it. – user2357112 supports Monica Mar 29 '14 at 13:33
• How is this sum of all characters or sumOfAllBytes. Isn't it just a count, rather than a sum? At best sumoflengthsof, but not sum of characters or bytes. – Bill Woodger Mar 31 '14 at 8:39
• You say sum of bytes, sum of characters. You are asking for neither of those things. It is a count of characters, or bytes, including delimiters, or a sum of the lengths, including delimiters. It is not the sum of bytes/characters. This is computers. – Bill Woodger Mar 31 '14 at 22:25

# GolfScript [4 bytes]

n*,)


There are no functions in GolfScript, so you should use a standard input instead:

["abc" "bdef" "cee" "deee"]
n*,)

>>> 18

• I need to learn GolfScript. :) – Jack Jun 21 '14 at 19:12

succ.length.unwords


Edit: Removed two bytes thanks to ais523!

• Hi, and welcome to PPCG! We allow submitting anonymous functions here; you don't need to name them. As such, you could remove the s= to save two bytes, as the submission evaluates to an expression of function type as it is. (I'm also curious as to whether there's a way to golf down the succ into something shorter, but all the possibilities I've thought of, such as (+1), are the same number of bytes or longer.) Incidentally, you can use # or ## at the start of a line for heading-bold (# is larger); that's the formatting most people use for their answers. – user62131 Jun 12 '17 at 22:18
• It also took me some time to figure out that this answer was correct. Often it's a good idea to include a link to an online interpreter that shows the program working. Here's one for your program; you could edit it into your answer. – user62131 Jun 12 '17 at 22:24
• By the way, don't worry about the downvote: it was caused by a bug in the Stack Exchange software, not made by an actual user. – user62131 Jun 12 '17 at 23:28
• You can use unlines instead of unwords and drop the succ, because unlines adds a final newline: Try it online! – Laikoni Jun 13 '17 at 6:59

# bash+coreutils - function body 10 bytes

sumOfAllBytes()(wc -c<<<$@)  Call as follows: $ a=("abc" "bdef" "cee" "deee")
$sumOfAllBytes${a[@]}
18
$ # Mathematica 31 29 bytes -2 bytes thanks to numbermaniac StringLength[""<>#]+Length@#&  Example StringLength[""<>#]+Length@#&@{ "abc", "bdef", "cee", "deee" }  18 • I don't think you need to assign the function name to a variable to be a valid answer, so you could save 2 bytes there. – numbermaniac Jun 13 '17 at 7:40 • @numermaniac, you are right. Thanks. – DavidC Jun 13 '17 at 12:02 # Rebol (body: 18 chars, function: 30 chars) 1 + length? form x  Usage example in Rebol console: >> f: func[x][1 + length? form x] >> f ["abc" "bdef" "cee" "deee"] == 18  # Perl, function body: 15 bytes sub sumOfAllBytes{!!@_+length"@_"}  Function sumOfAllBytes returns the sum of the string lengths including a "virtual" terminator byte. Perl does not have the concept of terminating a string with a null byte. A variant without a "terminating byte": sub sumOfAllBytes{$"=$;@_+length"@_"}  Byte count: The body of the functions are 15 or 17 bytes (with or without terminator). 7 bytes must be added, if the whole function with one-byte name should be counted. Test with degolfed version: sub sumOfAllBytes { # the argument(s) are put into array @_ by Perl !!@_ + # short for: @_ ? 1 : 0 # (if the array is empty nothing should be added) length "@_" # The array elements are interpolated into the double- # quoted string; the elements are separated by a space ($").
# without explicit return statement, the value of the last
# statement is returned.
}

@a = qw[abc bdef cee deee];
print sumOfAllBytes(@a), "\n"; # prints 18


## C, 78 minus declaration of a[]

Assumes 64 bit target and a[] containing at least one string.

char *a[]={"abc","bdef","cee","deee"};
f(int i){return strlen(a[i])+1+(i?f(i-1):0);}
main(){return f(sizeof(a)/8-1);}


To see the result, invoke thusly:

$./whatever || echo$?


# c preprocessor macros, 94 chars

The example in the question is not doable in c as a regular function as the function would have no idea the size of the array if just a pointer to the array is passed in. But we can do this as a couple of preprocessor macros:

#define c(s) sizeof(s)/sizeof(s[0])-1
#define sumOfAllBytes(s) s[c(s)]-s[0]+strlen(s[c(s)])+1


We assume that the array is declared, defined and initialised as in the question. That way the strings should be laid out sequentially in memory. We can then subtract the address of the first element from the last element and add the strlen() of the last element. No need to loop over all elements and strlen().

$./koe n = 18  # C - Loop:64, Recursion:56 (-18 for function body overhead) ## Loop s(char*a[],int n){int r=0;for(;n;)r+=strlen(a[--n])+1;return r;}  ## Recursion r(char*a[],int n){return n?strlen(a[0])+1+r(a+1,n-1):0;}  # PHP 5.3 - 60 array_sum(array_map(function($v){return strlen($v)+1;},$a));


Where $a is the array. • <?=strlen(join(_,$_GET))+1; should do the same – Jörg Hülsermann Jun 12 '17 at 21:40

# Fortran 90+ - 75 total, 23 for function alone

Unfortunately, Fortran doesn't allow for implicit typing of character strings, but we can abuse F77+F90 for golfing purposes. The whole program would be:

character*4::c(4)=["abc","bdef","cee","deee"];print*,sum(len_trim(c)+1);end


The function alone, written using a statement function would be

k(s)=sum(len_trim(s)+1)


which would be called using print*,k(c).

in C# 34 Excluding "string [] a=new [] { "abc", "bdef", "cee", "deee" };"

   string [] a=new [] { "abc", "bdef", "cee", "deee" };
var i=string.Join(" ",a).Length+1;
sol:2
var j =  string.Concat(a).Length + a.Length;


## Python - 5542 38

def s(v):print sum(1+len(x)for x in v)


# JavaScript - 42 characters without declaration

function s(x){return x.join(" ").length+1}


Usage:

a=["abc","bdef","cee","deee"];console.log(s(a));


## D - 58 54 bytes

int x(T)(T t){return reduce!"a+b.length"(t.length,t);}


This requires std.algorithm, so add the characters necessary for making that import if you want to.

Example of usage:

import std.stdio,std.algorithm;

int x(T)(T t){return reduce!"a+b.length"(t.length,t);}

void main(){
writeln(x!(string[])(["abc","bdef","cee","deee"]));
}


C#, 52 including variable declaration

int c=0;for(int i=0;i<a.Length;i++)c+=1+a[i].Length;


Uncompressed version for ease of reading:

int count = 0;
for (int i = 0; i < a.Length; i++)
count += 1 + a[i].Length;


## APL 6

+/1+⍴¨a


where

a←"abc" "bdef" "cee" "deee"


yields

      +/1+⍴¨a
18


Read as "Sum over 1 plus size of each element in a"

• Isn't that 6? +/1+⍴¨ Anyway, you can make it 4: ⍴⊢,∊ – Adám Jun 12 '17 at 20:16

# q/kdb+, 14 bytes

Solution:

sum 1+(#:)each


Example:

q)sum 1+(#:)each("abc";"bdef";"cee";"deee")
18


Explanation:

Evaluated right-to-left, thus:

// count (#:) length of each item in the list
q)(#:)each("abc";"bdef";"cee";"deee")
3 4 3 4
// add one to each item
q)1+ 3 4 3 4
4 5 4 5
// sum up the items
q)sum 4 5 4 5
18


# JavaScript - 20 17 Characters

s.join().length+1


Assumes that the array of strings is in variable 's' and outputs the answer to the console.

## Clojure, 32 bytes

#(apply +(map(comp inc count)%))


Composition FTW. Aww it is the same length as #(apply +(count %)(map count %)).

# F#, 56 bytes

Still practicing with F#...

let f a=(a|>Seq.map String.length|>Seq.sum)+Seq.length a


Invoke with:

[<EntryPoint>]
let main argv =
let sum = [| "abc"; "bdef"; "cee"; "deee" |] |> f
printfn "%A" sum
0

• Seq.sumBy(fun s->s.Length+1) – Asik Apr 25 '18 at 19:55

# Jelly (nc), 3 bytes

KL‘


Try it online!

I believe Jelly is (a lot) newer than the challenge, so I've marked this as non-competing.