CoderZone.org

Category: >> Perl Code >> Capitalize The First Word In A Sentence Bookmark and Share

<< lastnext >>

Snippet Name: Capitalize The First Word In A Sentence

Description: The following Perl snippet will capitalize the first word in a sentence.

Also see:
» Easy Password Encryption and Decry...
» Find first non-numeric character i...
» Word-Unwrap Function
» Smart Word Wrap Function
» Create an array from words in an H...
» Count all words in a string
» Regexp: Find and highlight repeate...
» Convert Dollars And Cents Value to...
» Reserved Words In Javascript
» Neatly trim a string to a set numb...
» Search All Words Of A String In my...
» USER password expiration
» USER password
» RegExp - Append first name first l...
» Count words or terms
» Bad Word Filter
» INSERT FIRST WHEN
» Date: Find first day of the month
» Spell out numbers to words
» Verify Password
» FIRST_VALUE
» FIRST
» Password setup
» Password Strength Tester
» Export table to Excel or MS Word f...
» Create Random Password
» Random Pronounceable Passwords
» Pronouncable password generator
» Output as Word Doc format

Comment: (none)

Author: CoderZone
Language: PERL
Highlight Mode: PERL
Last Modified: December 03rd, 2010

sub sentences{
         $sString = shift;
         $sString = lc($sString);
         $sString =~ s/ +/ /gs;
         @words = split(" ", $sString);
         $firstword = 0;
         $sNewString = "";
         $wordkey = 0;
 
         foreach $word(@words){
                 $word =~ s/ //gs;
                 $lastchar = substr($word, -1);
 
                 if($firstword || $wordkey == 0){
                         $word = ucfirst($word);
                         $firstword = 0;
                         $sNewString = $sNewString . " $word";
                 }elsif($lastchar eq '.' or $lastchar eq '!' or $lastchar eq '?'){
                         $firstword = 1;
                         $sNewString = $sNewString . " $word";
                 }else{
                         $sNewString = $sNewString . " $word";
                 }
 
         $sNewString =~ s/ +/ /gs;
         $sNewString =~ s/ i / I /gs;
         $sNewString = trim($sNewString);
         $wordkey++;
         }
         return $sNewString;
}
 
sub trim($){
        my $string = shift;
        $string =~ s/^\s+//;
        $string =~ s/\s+$//;
        return $string;
}
 
# sentences('NegoTiators woRked on a dEAl Thursday thAt would extend exPiring tax cuts.');
# will return "Negotiators worked on a Deal thursday that would extend expiring tax cuts."
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 15