4 December 2014

Quiz 68: Convert to uppercase and lowercase

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 str


Constraints: 

None

Sample Input

elePHAnTs

Sample Output:

elephants

Explanations:

more lowercase alphabets in original string, so convert to lowercase



Solution:

chomp($str=<STDIN>);
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

4 comments:

  1. How about: perl -e 'print lc('elePhAnTS')'

    or 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

    ReplyDelete
  2. Hi Alien
    We dont require to convert to lc everytime according to problem statement. We may need to convert to uc also sometimes.

    ReplyDelete
  3. Oh ok I just check input string and output string.
    Well, 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 :)

    ReplyDelete
  4. 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