Skip to main content
added 501 characters in body
Source Link
Khaled.K
  • 1.5k
  • 10
  • 12

Java 8, 113 chars

BigInteger C(String s){BigInteger i=BigInteger.ZERO;s.chars().forEach(x->i=i.add(BigInteger.valueOf());return i;}

Note each character in Java is Unicode 1-4 bytes unsigned integer, using sum function would easily overflow.

Detailed

BigInteger C (String s)
{
    BigInteger i = BigInteger.ZERO;

    s.chars().forEach(x -> i = i.add(BigInteger.valueOf(x));

    return i;
}

Java 8, 57 chars

ASCII only solution

int C(String s){return s.chars().filter(x->x<128).sum();} 

Detailed try here

public static int C (String s)
{
    return s
        .chars() // stream the characters
        .filter(x -> x < 128) // filter ASCII (optional)
        .sum(); // return the sum
}

Java 8, 57 chars

int C(String s){return s.chars().filter(x->x<128).sum();}

Detailed try here

public static int C (String s)
{
    return s
        .chars() // stream the characters
        .filter(x -> x < 128) // filter ASCII
        .sum(); // return the sum
}

Java 8, 113 chars

BigInteger C(String s){BigInteger i=BigInteger.ZERO;s.chars().forEach(x->i=i.add(BigInteger.valueOf());return i;}

Note each character in Java is Unicode 1-4 bytes unsigned integer, using sum function would easily overflow.

Detailed

BigInteger C (String s)
{
    BigInteger i = BigInteger.ZERO;

    s.chars().forEach(x -> i = i.add(BigInteger.valueOf(x));

    return i;
}

Java 8, 57 chars

ASCII only solution

int C(String s){return s.chars().filter(x->x<128).sum();} 

Detailed try here

public static int C (String s)
{
    return s
        .chars() // stream the characters
        .filter(x -> x < 128) // filter ASCII (optional)
        .sum(); // return the sum
}
Source Link
Khaled.K
  • 1.5k
  • 10
  • 12

Java 8, 57 chars

int C(String s){return s.chars().filter(x->x<128).sum();}

Detailed try here

public static int C (String s)
{
    return s
        .chars() // stream the characters
        .filter(x -> x < 128) // filter ASCII
        .sum(); // return the sum
}