ucwords functionality in Perl

Discussion in 'Perl' started by pradeep, Dec 5, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    There is a function ucwords() in PHP which capitalizes the first letter of each word.

    PHP:
    <?
     
    $str "programming forums";
     
    $str ucwords($str);
     
    // now $str = "Programming Forums"
     
    ?>
    I have written a sub-routine in Perl, which gives the same functionality.

    Code:
     sub ucwords
     {
       $str = shift;
       $str = lc($str);
       $str =~ s/\b(\w)/\u$1/g;
       return $str;
     }
     
     $str = "programming forums";
     $str = &ucwords($str);
     # now $str = "Programming Forums"
     
  2. #anna

    #anna New Member

    Joined:
    Oct 10, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.softsea.com/
    No bad code, useful to me, Thanks.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice