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.
Quote Reply
Re: [over644] Regular Expressions - Converting To Uppercase In reply to
Have a look here:

http://www.perldoc.com/...he-words-on-one-line-

----
Cheers,

Dan
Principal

Virtual Solutions
Quote Reply
Re: [over644] Regular Expressions - Converting To Uppercase In reply to
Something like this should work;

Code:
#!/usr/bin/perl

use strict;

#Capitalizes The First Letter Of Each Word In The Sentence.

print "Enter A Sentence You Want Capitalized: \n";
my $sentence = <STDIN>;

my $back;
my @list = split / /, $sentence;
foreach (@list) {
$back . = " " . ucfirst $_;
}
}
print $back;

This is untested, but it should work.

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates