Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created May 11, 2014 20:25
Show Gist options
  • Save dgodfrey206/9372ca6d76401a1aee5c to your computer and use it in GitHub Desktop.
Save dgodfrey206/9372ca6d76401a1aee5c to your computer and use it in GitHub Desktop.
A stream buffer that capitalizes every word of a sentence.
#include <iostream>
class stream_buffer : public std::streambuf
{
public:
stream_buffer(std::ostream& os)
: whitespace(true),
ctype(std::use_facet<std::ctype<char>>(getloc())),
m_sbuf(os.rdbuf())
{ }
int_type overflow(int_type c)
{
int_type res
= m_sbuf->sputc(whitespace ? std::toupper(c) : c);
whitespace = ctype.is(ctype.space | ctype.punct, c);
return res;
}
private:
bool whitespace;
const std::ctype<char>& ctype;
std::streambuf* m_sbuf;
};
int main()
{
stream_buffer sb(std::cout);
std::ostream os(&sb);
os << "hello my name is david.how are you";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment