Problem:
Given a Word consisting of both uppercase and lowercase aplhabets. Convert the whole string to either uppercase or lowercase such that number of alphabets which require conversions in minimum ie 'Hello' should be changed to 'hello' and 'HELlo' should be changed to 'HELLO'. If equal possibilty to convert to uppercase or lowercase, then change to lowercase.
Input Format:
String str
Output Format:
Uppercase str/Lowercase strConstraints:
NoneSample Input
elePHAnTs
Sample Output:
elephantsExplanations:
more lowercase alphabets in original string, so convert to lowercase
Solution:
while($str=~ /[A-Z]/g)
{
$u++;
}
$l=length($str)-$u;
if($l>=$u)
{
print "\L$str";
}
Tips:
use \U or \L to convert string to uppercase/lowercase
How about: perl -e 'print lc('elePhAnTS')'
ReplyDeleteor chomp(my $str=);
print lc($str);
For better and tricky question look at this quiz on use of map in real scenario.
http://www.aliencoders.org/forum/Thread-how-to-use-map-function-in-perl
Hi Alien
ReplyDeleteWe dont require to convert to lc everytime according to problem statement. We may need to convert to uc also sometimes.
Oh ok I just check input string and output string.
ReplyDeleteWell, try to put real case scenario questions where you really use in your IT life.
Asking these questions doesn't make sense to a real programmers :)
One part of this quiz may help programmer on how to calculate number of uppercase or lowercase characters occurring in a string. Other part, teaches them how to convert a string to uppercase/lowercase.
ReplyDelete