In: Computer Science
How To Print Nice Padding Character in a String in C++?
1. User input the width = 14
2. User input the padding character = +
Example output:
++Hello World++
Thanks in advance.
CODE
#include <iostream>
using namespace std;
int main() {
string input;
cout << "Enter the sentence: ";
getline(cin, input);
int width;
cout << "Enter the width: ";
cin >> width;
char padding;
cout << "Enter the padding character: ";
cin >> padding;
int paddingLength = (width - input.size() + 1) / 2;
for (int i=0; i<paddingLength; i++) {
cout << padding;
}
cout << input;
for (int i=0; i<paddingLength; i++) {
cout << padding;
}
return 0;
}