Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- #define CONSOLE_CHAR_WIDTH 80
- std::string& center_str(std::string& str, const char ch = ' ')
- {
- std::stringstream sstr(str);
- std::string tmpstr;
- std::string::iterator it;
- std::vector<std::string> words;
- int length;
- while (std::getline(sstr, tmpstr,'\n'))
- {
- for (it = tmpstr.begin(); it != tmpstr.end(); it++)
- {
- if (*it == '\t')
- {
- tmpstr.reserve(tmpstr.size() + 4);
- tmpstr.insert(it+1, 4, ch);
- tmpstr.erase(it);
- }
- }
- words.push_back(tmpstr);
- }
- sstr.clear();
- for (auto i : words)
- {
- length = i.length();
- tmpstr = i;
- i.clear();
- i.resize(CONSOLE_CHAR_WIDTH, ch);
- i.replace((CONSOLE_CHAR_WIDTH - length)/2, length, tmpstr);
- sstr << i;
- }
- str = sstr.str();
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement