Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok , There is a society in my college that deals with developing apps and some stuff like that! So here's the question they asked me during the Interview when i applied to their society!

for( blah blah )
{ cout<<"World";}

Replace something with "blah blah" phrase to print the output as :- Hello World !

At that moment i was utterly confused and still i am. Is there any way to do that. And I am only allowed to change the blah blah phrase only , nothing more than that. No code changes below or above that!
:(
Posted
Updated 12-Aug-13 5:32am
v2
Comments
pasztorpisti 12-Aug-13 11:58am    
They were interested in how well do you know the for loop. That's it. This question will become no question if you read enough C++ tutorials. Try learning by reading 1-2 good books on C++ instead of searching the answer for on such specific problem. By getting the answer to such specific questions without finding the answers on your own by investing energy you gain nothing. By solving it yourself you learn a lot and gain creativity. Well, I know that maybe your goal is something else, not becoming a C++ programmer...
Sergey Alexandrovich Kryukov 12-Aug-13 14:58pm    
Best answer so far.
—SA
AbhinavGauniyal 13-Aug-13 7:41am    
Recommend me some books , please. Because i dont know much books(good ones)
AbhinavGauniyal 13-Aug-13 7:46am    
The books that i have read , only focuses on the syntax of the language and more on theory type , but i want Practical type knowledge. Means the books through which i can strengthen my basics of C++. And thanks for the answer!

What can you replace "blah blah" with?
Well, three clauses, one that is executed before the bulk of the loop, one that tests if the loop should run, and one that gets executed after the loop.

So if you were to put
C++
cout << "Hello "
in the first clause, and
C++
cout << " !"
in the last, all you'd need to do is terminate the loop after the first iteration...

I'll leave the rest to you - it's your interview! :laugh:
 
Share this answer
 
Comments
AbhinavGauniyal 12-Aug-13 11:43am    
Are we allowed not to initialize something into those brackets! i mean like int i=something, i++ etc.
OriginalGriff 12-Aug-13 11:53am    
Try it! Best way to find out...:laugh:
[no name] 12-Aug-13 20:25pm    
I have solved this but I can't find a way to do it following your instruction.

If we use this nomenclature:
for ( init-expression ; cond-expression ; loop-expression )
statement

Then it seems you are saying init-expression contains cout << "hello " and
loop-expression contains cout << " !".

If you then try to also declare and initialise a variable in init-expression my compiler spits the dummy.

What am I missing? I think you can now reveal your solution. In fact to save time I will show you mine.

for( bool test = true ; test && cout << "Hello "; cout << " !", test = false)
cout << "world";
berrymaria 13-Aug-13 1:18am    
WOW. Nice :)
AbhinavGauniyal 13-Aug-13 7:44am    
Really good one!
I worked out another solution:

C++
for(cout<<"Hello World!"; false; )
    {
        cout<<"World";
    }
 
Share this answer
 
Comments
[no name] 13-Aug-13 1:12am    
Nothing wrong with a frontal attack. May cause a stir at the code review session.
H.Brydon 13-Aug-13 18:07pm    
+5 for being correct, yet bombastic.
for( blah blah )
{ cout << "World"; }

replace

blah blah

with

int i=0;i<1;i++ )
{
cout << "Hello ";
}
for ( int i=0;i<1;i++

If the questionmark really is meant to be, then this answer is wrong ofcourse

With questionmark the World from cout << "World" has to be overwritten with "World !" somehow,
maybe print "Hello World !" first then seek back to the pos of 'W' and write World again, but changes nothing in fact

C#
int i=0;i<1;i++ )
{
    cout << "Hello ";
}

cout.seekg( 6, ios::begin );
 
for ( int i=0;i<1;i++
 
Share this answer
 
v4
Comments
[no name] 13-Aug-13 0:20am    
May I respectfully refer you to OriginalGriff's solution and my comment on it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900