SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Strings

  1. #1
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Uk
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     <?php
     
    function check_string($string)
     {
         
    $valid false;
         
    $parts explode('-',$string);
         
    $sum 0;
         if(isset(
    $parts[0]) AND isset($parts[1]))
         {
             for(
    $i 0;$i strlen($parts[0]);++$i)
             {
             
    $sum $sum + (int)substr($parts[0],$i,1);
             }
             if(
    $sum == $parts[1])
             {
             
    $valid true;
             }
         }
             
         return 
    $valid;
     }
     
     
    $test '200065123-19';
     
    check_string($test) ? print('Valid') : print('Invalid');
     
    ?>

  3. #3
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice code, I did a few, not very important changes.
    PHP Code:
    function check_string($string)
    {
        
    $valid false;
        
    $parts explode('-',$string);
        
    $sum 0;
        if(isset(
    $parts[0]) AND isset($parts[1])) { 
            for(
    $i 0$c strlen($parts[0]); $i $c; ++$i) { // performance, calculate strlen ONCE
                
    $sum += (int)$parts[0]{$i}; // used shorter expression
            
    }
            if (
    $sum == $parts[1]) $valid true;
        }   
        return 
    $valid;
    }

    $test '200065123-19';
    check_string($test) ? print('Valid') : print('Invalid'); 

  4. #4
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no need to set a $valid variable when you can just return true or false. Plus the prints in the ternary are not needed. So I would do it like this.

    PHP Code:
        function check_string($string) {
            
    $parts explode('-',$string); 
            
    $sum 0;
            if(isset(
    $parts[0]) && isset($parts[1])) {
                for(
    $i 0$c strlen($parts[0]); $i $c$i++) { // performance, calculate strlen ONCE
                    
    $sum += (int)$parts[0][$i]; // used shorter expression
                
    }
                if (
    $sum == $parts[1]) {
                    Return 
    true;
                } else {
                    Return 
    false;
                }
            }
        }

        
    $test '200065123-19';
        echo (
    check_string($test)) ? 'Valid' 'Invalid'
    Plus I believe $i++ is a bit faster than ++i$.

    But that's all mostly personal preferance.

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no need to set a $valid variable when you can just return true or fales.
    Well, it's IMHO cleaner but of course, has the overhead of one extra variable.

    Plus I believe $i++ is a bit faster than ++i$.
    I'm not sure about it (in PHP) but since we don't need the incremented value in the expression I guess using $i++ makes more sense. As much as discussing this makes sense at all.

  6. #6
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow... thanks guys
    Good stuff!!


Bookmarks

Posting Permissions

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