hi i have a unknown string in c++ containing "\n" "\t" etc.
say;
string unknown1="\n\t\n\t\n\n\n\n\n";
now I want to print
"\n\t\n\t\n\n\n\n\n"
to the screen explcitly other than a bunch of empty spaces.... how should I do that? remember that I don't know what is in unknown1...
print "\n" explicitly in a string in c++
Page 1 of 16 Replies - 683 Views - Last Post: 29 April 2015 - 12:55 PM
Replies To: print "\n" explicitly in a string in c++
#2
Re: print "\n" explicitly in a string in c++
Posted 28 April 2015 - 08:01 PM
you need to escape the \ , the extra \ creates an '\' leaving the n normal
"\\n" would print "\n"
"\\n" would print "\n"
This post has been edited by infernorthor: 28 April 2015 - 08:02 PM
#3
Re: print "\n" explicitly in a string in c++
Posted 28 April 2015 - 08:02 PM
#4
Re: print "\n" explicitly in a string in c++
Posted 28 April 2015 - 08:05 PM
I get you now. You need create a for loop to print it out the way you want. check for those characters like
char c;
if( c == '\n' )
cout << "\\n";
#5
Re: print "\n" explicitly in a string in c++
Posted 28 April 2015 - 08:17 PM
infernorthor, on 28 April 2015 - 08:05 PM, said:
I get you now. You need create a for loop to print it out the way you want. check for those characters like
char c;
if( c == '\n' )
cout << "\\n";
but actually, when I started to learn c++. I remember there is a way to print strings explicitly... using some functions.... sometimes the string might contain something that I don't even know their existence, so I could not check every single characters.... for example there might be some char corresponding to \u that i don't know of.... Thank you:D
#6
Re: print "\n" explicitly in a string in c++
Posted 28 April 2015 - 08:22 PM
for those cases the only option is to print the number
cout << (int) c;
if you want the hex
#include <iomanip> and do
std::cout << std::hex << (int) c;
cout << (int) c;
if you want the hex
#include <iomanip> and do
std::cout << std::hex << (int) c;
#7
Re: print "\n" explicitly in a string in c++
Posted 29 April 2015 - 12:55 PM
The header <cctype> (ctype.h) has character classification routines.
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|