2

I want to be able to output multiple lines and have them all indented by a specified number of characters. So, if we have

int n = 3;

this would be the number of characters to indent, and then we had the string,

string s = "This is a string.\nThis is a string.\nThis is a string\n";

and I were to then output the string,

cout << s;

how could I make it so every line that is outputted is indented by n?

frontin
  • 733
  • 2
  • 12
  • 28
  • I'm courious to see the answer...basically the goas is mess with buffers and substiture \n with \n + n spaces... – marom Jun 02 '15 at 05:48
  • @buttifulbuttefly I was hoping there was a more straightforward solution, but alright, I will look into it. – frontin Jun 02 '15 at 05:49

2 Answers2

2

A crude workaround would be to find all instances of \n in the string to be printed and add a string containing blank space characters of specified length after every occurrence of "\n".

if ss is the string to be printed, and empty is a string containing blank space characters, then replace all instances of "\n" in ss by "\n" + empty

Refer to How to find and replace string? for code on doing this.

Depending on your application, you could convert this to a function, and try overloading cout to call it while printing any strings (not sure if it will work).

Community
  • 1
  • 1
therainmaker
  • 4,253
  • 1
  • 22
  • 41
  • So this is exactly what I was thinking and looking for, but with regards to the code, would `s.replace(s.find("\n"), 2, "\n" + spaces)` replace all instances of `\n` or just the first one? I didn't really get that clarification in the link you shared. – frontin Jun 02 '15 at 06:29
  • I think the top answer would only replace a single occurrence. You could look at the Boost library in the second answer if you want. Else, refer to this question: http://stackoverflow.com/questions/5343190/how-do-i-replace-all-instances-of-of-a-string-with-another-string – therainmaker Jun 02 '15 at 06:34
1

Initialize a string variable that will store the spaces, then add spaces to it by looping.

int n = 3;
string indents;

for (int c = 0; c < n; c++)
    indents += " ";

Combining everything in one string,

string s = "This is a string.\n" + indents + "This is a string.\n" + indents + "This is a string\n" + indents;

cout << s;

EDIT: Since you mentioned that the occurrences or positions of \n are unknown,

You can use string::find to find the first occurrence of \n, then add n spaces after it using string::insert then loop until all occurrences of \n are found and spaces are added after it.

int n = 3;
string s = "This is a string.\nThis is a string.\nThis is a string\n";

// first occurrence
size_t pos = s.find("\n");

while (pos != string::npos) {

    // insert n spaces after \n
    s.insert(pos + 1, n, ' ');

    // find the next \n
    pos = s.find("\n", pos + 1);
}
cout << s;

Output

This is a string.
   This is a string.
   This is a string.
raymelfrancisco
  • 828
  • 2
  • 11
  • 21
  • So I have gotten far enough in that I have used a similar idea to the spaces with `string spaces(n, ' ');` but the problem is that I won't know where the newlines are in s or if there will be any at all, so I am guessing that I will need to search through and find the index of all the \n characters and append the spaces. – frontin Jun 02 '15 at 06:23
  • If that's the case, you can take a look at this link: http://www.boost.org/doc/libs/1_42_0/doc/html/boost/algorithm/replace_all.html – raymelfrancisco Jun 02 '15 at 06:53