CGI Capitalize the first word

Capitalize the first word

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;
}