Statistics

Problem Statement for "CenterString"

Problem Statement

PROBLEM STATEMENT

You are coding a bunch of new reports for your system.  The client has asked
that the report title be centered on each page.  You've decided to write a
function that will take the report title and return it centered within a field
of a specified length.

Implement a class CenterString, which contains a method center.  This method
will return a string of specified length with the input String centered within
it.  Since you are centering a report title, eliminate (trim) all leading and
trailing spaces from the reportTitle prior to centering it.  If the passed
string cannot be centered exactly, position the string one character to the
left of true center (see examples 2 & 3 below).  For readability of results -
if the input String is less than the specified length, pad the sides (whether
left, right or both) with dashes ("-").

DEFINITION

Class name: CenterString
Method name: center
Parameters: String, int
Returns: String

The method signature is:
String center(String reportTitle, int outStringLen);
Be sure your method is public.

TopCoder will ensure the validity of the inputs. Inputs are valid if all of the
following criteria are met:
*The reportTitle will be from 0 to 50 characters, inclusive (after trimming
leading and trailing spaces).
*The reportTitle will consist only of letters (a-z, A-Z) and/or numbers (0-9,
inclusive) and/or spaces (' ').
*The outStringLen will be from 1 to 50, inclusive.
*The outStringLen will be greater than or equal to the length of the
reportTitle (after trimming leading and trailing spaces).

NOTES
*The returned field should be padded (on both sides) with dashes "-" for
readability (see the examples).
*If the string cannot be centered exactly, it should be offset to the left by
one position from the true center (see examples 2 and 3 below).
*The returned field should have the same case as the input.


EXAMPLES
-If the reportTitle is "TopCoder Earnings for 2001" and the outStringLen is 30,
then the result should be "--TopCoder Earnings for 2001--".
-If the reportTitle is "TopCoder Earnings for 2001" and the outStringLen is 29,
then the result should be "-TopCoder Earnings for 2001--".
-If the reportTitle is "Another Boring Summary Report" and the outStringLen is
34, then the result should be "--Another Boring Summary Report---".	
-If the reportTitle is " Trim This " and the outStringLen is 11, then the
function returns "-Trim This-".
-If the reportTitle is "" and the outStringLen is 1, then the function returns
"-".
-If the reportTitle is "    " and the outStringLen is 1, then the function
returns "-".

Definition

Class:
CenterString
Method:
center
Parameters:
String, int
Returns:
String
Method signature:
String center(String param0, int param1)
(be sure your method is public)

Constraints

    Examples


      This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
      This problem was used for: