Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   ucwords functionality in Perl (http://www.go4expert.com/articles/ucwords-functionality-perl-t2138/)

pradeep 5Dec2006 17:41

ucwords functionality in Perl
 
There is a function ucwords() in PHP which capitalizes the first letter of each word.

PHP Code:

<?
 $str = "programming forums";
 $str = ucwords($str);
 // now $str = "Programming Forums"
 ?>

I have written a sub-routine in Perl, which gives the same functionality.

Code: Perl

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"


#anna 10Oct2007 10:57

Re: ucwords functionality in Perl
 
No bad code, useful to me, Thanks.


All times are GMT +5.5. The time now is 03:16.