6 Replies - 683 Views - Last Post: 29 April 2015 - 12:55 PM Rate Topic: -----

#1 key01023   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 30-September 14

print "\n" explicitly in a string in c++

Posted 28 April 2015 - 07:44 PM

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...

Is This A Good Question/Topic? 0
  • +

Replies To: print "\n" explicitly in a string in c++

#2 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

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"

This post has been edited by infernorthor: 28 April 2015 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 key01023   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 30-September 14

Re: print "\n" explicitly in a string in c++

Posted 28 April 2015 - 08:02 PM

View Postinfernorthor, on 28 April 2015 - 08:01 PM, said:

you need to escape the \ , the extra \ creates and \ leaving the n normal

"\\n"



That is just an example.... I don't know what is inside unknown_string...... I just want to visualize the string explicitly....
Was This Post Helpful? 0
  • +
  • -

#4 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

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"; 


Was This Post Helpful? 0
  • +
  • -

#5 key01023   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 30-September 14

Re: print "\n" explicitly in a string in c++

Posted 28 April 2015 - 08:17 PM

View Postinfernorthor, 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
Was This Post Helpful? 0
  • +
  • -

#6 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

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;
Was This Post Helpful? 0
  • +
  • -

#7 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1