Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Strings

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Strings

    I recive a string in a form like this: 200065123-19
    If you add all the numbers before the dash together, the sum shall be the same as the number after the dash. Like this: 2+0+0+0+6+5+1+2+3 = 19

    How can I check this?
    Last edited by Pompiuses; 01-12-2004 at 05:52 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Split the string on the dash into two parts. Then, create an array out of the pre-dash part containing where each digit is in a field. After that you only need to sum up the fields of that array and compare that to the value right of the dash.

    PHP Code:
    function checksum($word) {
        list(
    $numbers$result) = split("-"$word);
        
    $calcSum array_sum(preg_split("//"$numbers));
        return 
    $calcSum == $result;
    }

    print 
    checksum('200065123-19'); 
    De gustibus non est disputandum.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •