See Out "cout" In C++

Desired output:

Hello world!
Zeeshan says, "It's I who can do it!"

What is the length of the string My_Stng used to print, exactly, the above statement using cout<<My_Stng; command only once?

Details and Assumptions :

  • Space is also considered as a character.

  • There is no space before or after both the statements.


The answer is 50.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Zeeshan Ali
Feb 20, 2016
1
2
string My_Stng="Hello world!\nZeeshan says, \"It's I who can do it!\"";
cout<<My_Stng;   // cout is used  to get sth displayed or printed

The above code script gives you the desired output, to get the length of that string, simply, use the builtin function length() with string name as My_Stng.length() . Actually, cout<<"Hello world!"; is enough to print "Hello world!" , but to print a new line, a character \n is used. Moreover, if you want to print the character " , you have to use a slash \ with it to avoid an error because that symbol " is both the starting and the ending of a string.

Compilers treat both \n and \" as a single character. Hence My_Stng.length() returns 50 \boxed{50} .

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...