27 November 2014

Quiz 56:Make the text center align

Problem:

You are give N words. Length of each word<=10. You need to make them center aligned keeping max length as 10 ie a 6 letter word should be changed to 10 letter word by adding 2 spaces to right and 2 to left. If length is odd, then keep the word towards left.

Input Format: 

N=number of words
N lines containing a word

Output Format: 

N lines containing center aligned word


Constraints: 

None

Sample Input

10
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa

Sample Output:

    a
    aa
   aaa
   aaaa
  aaaaa
  aaaaaa
 aaaaaaa
 aaaaaaaa
aaaaaaaaa
aaaaaaaaaa

Explanations:

O/P is center aligned


Solution:

chomp($n=<STDIN>);
for($i=0;$i<$n;$i++)
{
chomp($in=<STDIN>);
@arr=split(//,$in);
$len=@arr;
if($len%2==0)
{
$space_r=(10-$len)/2;
$space_l=$space_r;
}
else
{
$space_r=(10-$len+1)/2;
$space_l=$space_r-1;
}
$ans=(' ' x ($space_l).$in.' ' x ($space_r));
push(@out,$ans);
}
foreach(@out)
{
print "$_\n";
}



Tips:

' ' is used to insert space

No comments:

Post a Comment