What code will run this loop?

So, here is the question asked during the interview -

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

my task was to replace blah blah so that the printed result was :- Hello World !

And I am only allowed to change the blah blah phrase, nothing more. No code changes below or above this!

+3
c ++ loops
source share
8 answers

There are two problems.

  • Output in loop title
  • Suppress iteration.

In the first part of the for declaration, you can execute almost every command. Therefore, it’s nice to print the desired result.

To suppress an iteration, simply enter false as a condition. The last part of the header can be left blank.

 #include <iostream> using namespace std; int main() { for ( cout<<"Hello World!"<<endl ; false ; ) { cout<<"World"<<endl; } return 0; } 
+4
source share

Why even bother with the body of the for loop if you can put something in blah blah :

 for(int i = 0 ; std::cout << "Hello world!", i < 0; ) { std::cout<<"World"; } 
+14
source share

They didn’t say you need to start a loop, right?

 #include <iostream> #include <ios> using namespace std; int main() { for (int i = (cout << "Hello World!",0); i; ) { cout << "World"; } } 
+7
source share

It took me a bit of time trying to control the loop until one iteration:

 #include <iostream> #include <ios> using namespace std; int main() { for (cout << "Hello "; cout; cout.setstate(ios::badbit)) { cout << "World"; } } 

If ! intended for output, then this code will do this:

 #include <iostream> #include <ios> using namespace std; int main() { for (cout << "Hello "; cout; cout << "!", cout.setstate(ios::badbit)) { cout << "World"; } } 
+6
source share

How about this for blah blah :

 const char *p = "Hello "; *p; cout << *p, p++); if (1 
+2
source share

Obviously, a for loop is nothing more than a leak. Look at it like this:

 for( /* Your Code Here */ ) { cout << "World"; } 

So, the first thing you need to do is get rid of all the annoying code that is already in the program:

 for( /* Your Code Begins */ ;0;); /* TODO */ if(0 /* Your Code Ends */ ) { cout << "World"; } 

Since, naturally, they will want to test your intelligence, let's do something very smart as follows:

  for( /* Your Code Begins */ ;0;); struct cout { void operator<<(cout world) { hello <world> !""; } char const* out; cout() : out(0) { } cout(char const* out) : out(out) { } operator bool() { return !out; } struct typeof { typeof(decltype(::std::cout) const&) { } void operator>(bool a) { ::std::cout << (!a ? "!" : "?"); } }; struct { typeof operator<(cout c) { return typeof(::std::cout << "Hello " << c.out); } } hello; } cout; if(cout /* Your Code Ends */ ) { cout << "World"; } 

Note. Since they like the name cout , be sure to use it in your code!

Also note the common GCC error (well, they call it the β€œ extension ”), which can cause problems with your C ++ compatible code.

Finally, recall that the initial instructions did not call for any new line at the end, so don't expect it. If you want to see the result right away, you can flush your output.

Some of the hoops that you should skip may seem unnecessary, but the strict requirement that you use a nested class instead of one at the namespace level causes some problems with proper operator overloading.

Personally, I would like to note that this code is very easy to understand: everything that it does says hello <world>! , so let's just reformat the result a bit, which will lead to your final code (this time the surrounding constructs are omitted):

  ;0;);struct cout{void operator<<(cout world){ hello <world>! "";}char const*out;cout():out(0){}cout(char const*out):out(out){} operator bool(){return!out;}struct typeof{typeof(decltype(::std::cout)const&){}void operator>(bool a){::std::cout<<(!a?"!":"?");}}; struct{typeof operator<(cout c){return typeof(::std::cout<<"Hello "<<c.out);}}hello;}cout;if(cout 

Of course, the last step is taken only for purely aesthetic reasons and can be omitted if necessary.

PS: In fact, he prints β€œHello World!”, And not β€œ: - Hello World!”, Which, I assume, is exactly what it was said. Changing it to a different format remains as an exercise for the reader.

+2
source share

Use the fact that C ++ does not recognize spaces!

 for(int i = 0; i < 0; ++i); if(true){ cout << "Hello World!"; } else { //blah blah ) { cout<<"World"; } 
0
source share

No one bothered to fix the obviously broken code, access to such a mysterious undefined cout , which is a very obvious horrible code, so ... here's the fix:

 #include <iostream> #define BLAH \ struct { \ template <typename T> \ void operator<<(T const& t) const { \ ::std::cout << ":- Hello " << t << " !\n"; \ } \ bool okay;\ } cout{true}; \ cout.okay; \ cout.okay = false int main() { for(BLAH) { cout<<"World"; } } 

Just for better readability, which I share, can you switch to a separate #define called BLAH .

Note. My gcc refuses to build this, but clang accepts this with the highest level of warning. I build hw.cpp as follows:

 make CXXFLAGS=-Wall\ -Wextra\ -pedantic\ -std=c++11 CXX=clang++ hw 
0
source share

All Articles