How to pad strings in C++
This post will discuss how to pad strings in C++.
1. Using std::setw
The std::setw manipulator is commonly used to set the field width in C++ output operations. It is declared in the header <iomanip>. We can use it with std::ostringstream to pad a string with leading zeros.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> #include <iomanip> #include <sstream> std::string getPaddingString(std::string const &str, int n) { std::ostringstream oss; oss << std::setw(n) << str; return oss.str(); } int main() { std::string str = "ABC"; int n = 5; std::string paddedString = getPaddingString(str, n); std::cout << "\'" << paddedString << "\'"; // ' ABC' return 0; } |
If you need to just print the padded string to the output stream, do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> #include <iomanip> int main() { std::string s = "ABC"; int n = 5; std::cout << "'" << std::setw(n) << s << "'\n"; // ' ABC' return 0; } |
2. Using std::setfill
Another option is to use the std::ostringstream with the std::setfill function, which sets the specified character as the fill character for the stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <string> #include <iomanip> #include <sstream> std::string getLeftPaddingString(std::string const &str, int n, char paddedChar = ' ') { std::ostringstream ss; ss << std::right << std::setfill(paddedChar) << std::setw(n) << str; return ss.str(); } std::string getRightPaddingString(std::string const &str, int n, char paddedChar = ' ') { std::ostringstream ss; ss << std::left << std::setfill(paddedChar) << std::setw(n) << str; return ss.str(); } int main() { std::string str = "ABC"; int n = 5; std::cout << getLeftPaddingString(str, n, '0') << std::endl; // 00ABC std::cout << getRightPaddingString(str, n, '0') << std::endl; // ABC00 return 0; } |
3. Using string::insert
The string::insert(i, n, c) inserts n consecutive copies of character c into the string before index i. We can use it to add padding to a string, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #include <string> void getLeftPaddingString(std::string &str, int n, char padChar = ' ') { if (n <= str.size()) { return; } str.insert(0, n - str.size(), padChar); } void getRightPaddingString(std::string &str, int n, char padChar = ' ') { if (n <= str.size()) { return; } str.insert(str.size(), n - str.size(), padChar); } int main() { std::string str = "ABC"; int n = 5; getLeftPaddingString(str, n, '0'); std::cout << str << std::endl; // 00ABC return 0; } |
4. Using std::string constructor
Finally, we can use the string constructor to construct only the padding and then append/prepend padding to the original string using the + operator. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> #include <string> #include <iomanip> #include <sstream> std::string getRightPaddingString(std::string const &str, size_t s, char paddedChar = ' ') { if (str.size() < s) { return str + std::string(s - str.size(), paddedChar); } else { return str; } } std::string getLeftPaddingString(std::string const &str, size_t s, char paddedChar = ' ') { if (str.size() < s) { return std::string(s - str.size(), paddedChar) + str; } else { return str; } } int main() { std::string str = "ABC"; int n = 5; std::cout << getLeftPaddingString(str, n) << std::endl; std::cout << getRightPaddingString(str, n) << std::endl; return 0; } |
That’s all about padding strings in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)