Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:37:11+00:00 2026-06-18T08:37:11+00:00

Suppose I want to rot13 every string passed to cout (or another ostream), so

  • 0

Suppose I want to rot13 every string passed to cout (or another ostream), so that, say cout<<"Foo Bar Baz.;" (or even cout<<rot13<<"Foo Bar Baz.";) outputs Sbb One Onm.

How would I go about doing that?

(My first idea was to replace cout’s streambuf with a streambuf-derived class that would do all the work. But seeing as the original streambuf is responsible for directing stuff to the console… that didn’t work out at all.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:37:12+00:00Added an answer on June 18, 2026 at 8:37 am

    You can write your own stream that overload operator<< for char*, std::string and others and prints a transformed text.

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <iterator>
    
    using namespace std;
    
    class ostream_rot13 : public basic_ostream <char, char_traits<char> >
    {
    public:
        ostream_rot13(std::basic_streambuf<char, char_traits<char> >* sb) 
        : basic_ostream<char, char_traits<char> >(sb) {}
    
        ostream_rot13& operator<<(const char* text)
        {
            std::string s(text);
    
            int rot=13;
            std::transform(std::begin(s), std::end(s), ostream_iterator<char>(*this), [rot] (char c) { 
                if(c >= 'a' && c <= 'z')
                    return 'a' + (c + rot - 'a') % 26;
                else if(c >= 'A' && c <= 'Z')
                    return 'A' + (c + rot - 'A') % 26;
    
                return (int)c;
            });
    
            return *this;
        }
    };
    

    The next step is to declare a global variable of this type and then a macro that replaces cout with the new variable.

    ostream_rot13 cout_rot13(std::cout.rdbuf());
    
    #define cout cout_rot13
    

    And then all instances of cout will become cout_rot13.

    int main() 
    {
        cout << "Foo Bar Baz";
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i want to create a method that takes a number as a string
Suppose I want a string, say 123, to fill a given rectangle, like so:
Suppose I want to put objects that identify a server into a stl set
Suppose I want to create a dictionary that maps digits to numbers less than
Suppose I want to add several numbers together like: 1. Find even numbers between
Suppose I want to create a map in vim that will call an ex
Suppose I want to run a Mathematica program that I wrote, in a cluster
Suppose I want to create trees of a certain set depth, that is, the
Suppose I want to wrap code that can throw exceptions with a try-catch block
Suppose I want to have a method that passes the BeginInvoke method of an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.