Menu
Coddy logo textTech

Center Text

Lesson 6 of 18 in Coddy's Functions in C++: Building Your Own Functions course.

challenge icon

Challenge

Easy

Write 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:

Coddytech

Try it yourself

#include <iostream>
using namespace std;

string center( ) {
    
    
}

All lessons in Functions in C++: Building Your Own Functions