Center Text
Lesson 6 of 18 in Coddy's Functions in C++: Building Your Own Functions course.
Challenge
EasyWrite a function to center a string by padding it with a specific character on both sides.
The center_string function takes a string, a specific character, and an integer as the total width. The function centers the string by padding it with the specified character on both sides. If the total width is less than or equal to the length of the string, the original string should be returned without any padding.
For example:
If the input string is "Coddy", the character is "*", and the total width is 10, the output will be:
**Coddy***If the input string is "Coddytech", the character is ".", and the total width is 9 (equal to the length of the string), the output will be:
CoddytechTry it yourself
#include <iostream>
using namespace std;
string center( ) {
}
All lessons in Functions in C++: Building Your Own Functions
1Introduction
Introduction2Beginner-level function
Repeat a StringReverse a StringMax ValueFill stringCenter TextFormat TextMath Operation