Menu
Coddy logo textTech

Fill string

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

challenge icon

Challenge

Easy

Write a function to fill a string with a specific character.
The fill_string function takes a string, a specific character, and an integer as the width. The function fills the string with the specified character to match the given width.


For example, if the input string is <strong>"1234"</strong>, the character is <strong>"@"</strong>, and the width is 6, the output will be:

 @@1234

If the passed width is smaller or equal to the length of the original string, the function will print the message "String is already long enough" and return without modifying the string.

Try it yourself

#include <iostream>
using namespace std;

void fill(  ) {
    
   
}

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