Gossamer Forum
Home : General : Perl Programming :

Regular Expressions - Converting To Uppercase

Quote Reply
Regular Expressions - Converting To Uppercase
I'm trying to write a perl program that will convert the first letter of all words to uppercase. The problem is there are functions uc() and ucfirst() but uc() capitalizes everything and ucfirst() only capitalizes the first character of the string. I tried to write a program to give myself ideas on how to do this, but I am stumped at the moment.

#!/usr/bin/perl
#Capitalizes The First Letter Of Each Word In The Sentence.
print "Enter A Sentence You Want Capitalized: \n";
$sentence = <STDIN>;
@list = $sentence;
for( $i = 0; $i < @list; $i++) {
if ( $list[ $i ] == " " ) {
ucfirst $list[ $i + 1 ];
}
}
print "@list";

Basically my idea in this program is to search the array for a space and when it finds one, it moves one character to the right which should logically contain a word, and then it applies the uppercase thing to it. Any thoughts or opinions are much appreciated.
Subject Author Views Date
Thread Regular Expressions - Converting To Uppercase over644 9482 Apr 14, 2004, 2:04 PM
Post Re: [over644] Regular Expressions - Converting To Uppercase
dan 9310 Apr 14, 2004, 2:15 PM
Post Re: [over644] Regular Expressions - Converting To Uppercase
Andy 9261 Apr 15, 2004, 3:38 AM