Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Reversing string case

by nvivek (Vicar)
on Aug 25, 2010 at 11:36 UTC ( #857159=note: print w/ replies, xml ) Need Help??


in reply to Reversing string case

Following code will help you.

use strict; use warnings; print "Enter the string:"; my $string=<STDIN>; #getting the input from user chomp($string); #remove the newline at end in string if($string=~/^[a-z]+$/) { #checking whether the string is in lowercase print "String given in Lower Case:$string\n"; $string=~s/([a-z])/\u$1/g; #replacing the string to be in upper case print "Uppercase String:$string\n"; } elsif($string=~/^[A-Z]+$/) { #checking whether the string is in upperc +ase print "String given in Upper Case:$string\n"; $string=~s/([A-Z])/\l$1/g; #replacing the string to be in lower case print "Lowercase String:$string\n"; } elsif($string=~/^[a-zA-Z]+$/) { #checking whether the string is in mix +ed case print "String given in Mixed Case:$string\n"; $string=~s/([a-z])/\u$1/g; #replacing the string to be in upper case print "Uppercase String:$string\n"; $string=~s/([A-Z])/\l$1/g; #replacing the string to be in lower case print "Lowercase String:$string\n"; }


Comment on Re: Reversing string case
Download Code
Re^2: Reversing string case
by Anonymous Monk on Aug 25, 2010 at 12:19 UTC

    Your mixed case test will not work the way you intended

    /[a-zA-Z]/ matches any lower- or uppercase character, not a lower- and then an uppercase one.

    And your two substitutions will not flip the case of a character because after the first substitution (i.e. s/([a-z])/\u$1/g) all characters are uppercase and will be changed to lowercase at the second substitution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://857159]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-07-22 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (117 votes), past polls