How do I print "\n" explicitly in a string in c++ -
hi have unknown string in c++ containing "\n" "\t" etc. say;
string unknown1=read_from_file();
if unknown1="\n\t\n\t\n\n\n\n\n" want print
"\n\t\n\t\n\n\n\n\n"
to screen explcitly other bunch of empty spaces.... how should that? remember don't know in unknown1...
to emphasize, know print \n explicitly if change "\n" "\n" every such character... problem don't know inside unknown1... read file....
thanks answering, have further concerns:
the procedure answered has 1 more porblem... suppose don't know possible special character in file \n or \t... maybe there \u ? \l ? think can't exhaust every possibility right? there kind of built in c++ function output corresponding characters?
\n
, \t
escape sequences, can print them adding \
before them, \\
used obtain single backslash. single backslash means escape sequence ( if valid 1 ), 2 backslashes represent backslash character, whenever need output backslash, add 2 of them.
so, if use
string unknown1="\\n\\t\\n\\t\\n\\n\\n\\n\\n";
you desired output.
if reading file , this
string unknown1="\n\t\n\t\n\n\n\n\n"; ( int = 0 ; < unknown1.length() ; i++ ) { if( unknown1[i] == '\n') cout<<"\\n"; }
like that, have check each escape sequence may used.
Comments
Post a Comment