7 kyu

"Center yourself", says the monk.

912 of 1,605siebenschlaefer

Description:

Your company MRE Tech has hired a spiritual consultant who advised on a new Balance policy: Don't take sides, don't favour, stay in the middle. This policy even applies to the software where all strings should now be centered. You are the poor soul to implement it.

Task

Implement a function center that takes a string strng, an integer width, and an optional character fill (default: ' ') and returns a new string of length width where strng is centered and on the right and left padded with fill.

center(strng, width, fill=' ')
center(strng, width, fill=' ')
char *center(const char *strng, size_t width, char fill)

If the left and right padding cannot be of equal length make the padding on the left side one character longer.

If strng is longer than width return strng unchanged.

Examples

center('a', 3)  # returns " a "
center('abc', 10, '_')  # returns "____abc___"
center('abcdefg', 2)  # returns "abcdefg"
center('a', 3)  # returns " a "
center('abc', 10, '_')  # returns "____abc___"
center('abcdefg', 2)  # returns "abcdefg"
center("a", 3, ' ')  // returns " a "
center("abc", 10, '_')  // returns "____abc___"
center("abcdefg", 2, ' ')  // returns "abcdefg"
      Center("a", 3, space)  => result = " a "
      Center("abc", 10, '_')  => result = "____abc___"
      Center("abcdefg", 2, space)  => result = "abcdefg"
Strings
Fundamentals

Stats:

CreatedJul 16, 2017
PublishedJul 16, 2017
Warriors Trained3051
Total Skips82
Total Code Submissions7609
Total Times Completed1605
Python Completions912
C Completions122
JavaScript Completions613
COBOL Completions3
Total Stars53
% of votes with a positive feedback rating91% of 329
Total "Very Satisfied" Votes280
Total "Somewhat Satisfied" Votes41
Total "Not Satisfied" Votes8
Total Rank Assessments12
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • siebenschlaefer Avatar
  • zruF Avatar
  • FArekkusu Avatar
  • trashy_incel Avatar
  • kirull Avatar
  • akar-0 Avatar
  • XoRMiAS Avatar
Ad