There are quite a few ways to do this, but here's one that's fairly simple and easy.
#include <sstream>
#include <string>
#include <iostream>
void wrap(std::string const &input, size_t width, std::ostream &os, size_t indent = 0) {
std::istringstream in(input);
os << std::string(indent, ' ');
size_t current = indent;
std::string word;
while (in >> word) {
if (current + word.size() > width) {
os << "\n" << std::string(indent, ' ');
current = indent;
}
os << word << ' ';
current += word.size() + 1;
}
}
int main() {
char *in = "There was a time when he would have embraced the change that was coming. In his youth he"
" sought adventure and the unknown, but that had been years ago. He wished he could go back and learn"
" to find the excitement that came with change but it was useless. That curiousity had long ago left"
" him to where he had come to loathe anything that put him out of his comfort zone.";
wrap(in, 72, std::cout, 0);
return 0;
}
This implicitly assumes that you want to remove an extra white space that might be present in your input string, so if (for example) you started with your text formatted for 60 columns with a 5-character indent:
There was a time when he would have embraced the change
that was coming. In his youth he sought adventure and
the unknown, but that had been years ago. He wished he
could go back and learn to find the excitement that
came with change but it was useless. That curiousity
had long ago left him to where he had come to loathe
anything that put him out of his comfort zone.
...but you asked for it to be word-wrapped at 72 columns with no indentation, it would get rid of the white space being used for the previous indentation, so it would come out like this:
There was a time when he would have embraced the change that was coming.
In his youth he sought adventure and the unknown, but that had been
years ago. He wished he could go back and learn to find the excitement
that came with change but it was useless. That curiousity had long ago
left him to where he had come to loathe anything that put him out of his
comfort zone.
Another possibility, especially if you want to word-wrap all the output to a particular stream, would be to use the word-wrapping streambuf I posted in another answer.