Sunday 19 October 2014

Center Align Text

Level: Easy

Given an input string S, center justify the string by using the character '_' to align the string. There will be neither preceding spaces before the string S nor suffix spaces after the string S. The output should be center-justified in a line of width 64 characters, followed by a newline. 
Note:
1. If S has an odd number of characters then the number of preceding '_' should be one more than the number of trailing '_'
2. If S has an even number of characters then the number of preceding '_' should be equal to the number of trailing '_'

Input Format:
First line is the input string S s.t. 1 <= |S| <= 64



Output Format: 
Output the center justified string S followed by a newline. 


#include<stdio.h>
#include<string.h>
int main()
{int n,a,dif,i;char no[64];
scanf("%[^\n]s",&no);
n=strlen(no);
if(n%2==0)
{
a=(64-n)/2;
}
else
{
a=(65-n)/2;
}

dif=a+n;
for(i=0;i<64;i++)
{

if(i<a)
printf("_");

else if(i<dif)
{printf("%s",no);
i=dif-1;}

else
printf("_");
}

return 0;

}

No comments:

Post a Comment