The Shortest.. Solved Again [Problem of the Week]

it’s time to show you the solutions submitted to this problem..

I have heard about this problem once from Abdalla Gamal, The acm Guru that taught us every thing we know about acming.. then i found it later mentioned in a book

i recommend that you copy all the code in a cpp, comment it, then un-comment and test each one alone.

so .. first there were solutions by Mohamed Samy, Mohamed Adbelghani, Hatem Adbelghani and Tasniem they used Printf

//m samy
#include <stdio.h>
int main(){char q='"',nl='\n',bs='\\',p='%';char *s="#include<stdio.h>%cint main(){char q='%c',nl='%cn',bs='%c%c',p='%c';char *s=%c%s%c;printf(s,nl,q,bs,bs,bs,p,q,s,q);}";printf(s,nl,q,bs,bs,bs,p,q,s,q);}

//m adbelghani
char* printedCode="#include<stdio.h>%cchar* printedCode= %c%s%c;%cvoid main()%c{%cprintf(printedCode,10,34,printedCode,34,10,10,10,10);%c}";
void main()

char prog[]="void main(){printf(_char prog[]=%c%s%c;_,34,prog,34);for(char*s=prog;*s;s++)*s=(*s==95?34:*s);printf(_%s_,prog);}";void main(){printf("char prog[]=%c%s%c;",34,prog,34);for(char*s=prog;*s;s++)*s=(*s==95?34:*s);printf("%s",prog);}

#include <stdio.h>
char *myProg = "#include<stdio.h>%cchar *myProg=%c%s%c;%cvoid main(){printf(myProg,10,34,myProg,34,10,10);}%c";
void main(){printf(myProg,10,34,myProg,34,10,10);}

Then there was Jaqoup and Tecno who both used Macros

#include <iostream>
using namespace std;
#define ENIUQ(TEMPLATE) cout << TEMPLATE << "(" << #TEMPLATE << ");\n}";

void main()
    ENIUQ("#include <iostream>\nusing namespace std; \n#define ENIUQ(TEMPLATE) cout << TEMPLATE <<  \"(\"  << #TEMPLATE << \");\\n}; \n \nvoid main() \n{\n   ENIUQ");

#include <iostream>
using namespace std;

#define STR(str)\
    cout <<"int main(){\nSTR(" << #str<<")\n}";\
int main()
        cout << "this is test program";
    return 0; )

Tecno’s Solution although not 100% accurate.. is very elegant.

Then a solution by Mohamed Abd El-Mon’em (Harry Potter), he used cout with no Macros which is why i like his solution alot.

//harry potter
char *str="#include <iostream> using namespace std; int main(){cout<<char(99)<<char(104)<<char(97)<<char(114)<<char(32)<<char(42)<<char(115)<<char(116)<<char(114)<<char(61)<<char(34)<<str<<char(34)<<char(59)<<str;}";
#include <iostream>
using namespace std;

int main()

Then i want you to take a look at another two solutions by Alaa Shaker and Tasniem..

#include <iostream>
using namespace std;

int main()
    // Write ANYTHING .. ANYWHERE!
    FILE *file = fopen(__FILE__, "r");

    if (!file) return 1;

    for(int ch = fgetc(file); ch != EOF; ch = fgetc(file))
        printf ("%c", ch);


    // Thanks to Abdallah Gamal .. LOL
    return 0;

using namespace std;

void main ()
    ifstream fin(__FILE__);
    char a;
    while( !fin.eof())
        fin.get( a );

.. shocking .. hah ?! 😀

.. both though totally outside the box.. simple, straight forward .. and even prints comments as they said 😀

Thanks to every one.. 😀 .. i hope you liked that problem

The Shortest (again!) [Problem of the Week]

Once that a blogger says “i am too busy to post and i will start posting regularly once i am free” he never actually becomes free and never it happens that you see a post in his blog again..

so i wont say that .. but since i am busy a bit .. i will leave you with a strange problem this time.. famous among hackers.. its about..

The Shortest Program That Can Print It Self

..ignoring header files..

so lets say you write this program

#include <stdio.h>void main(){}

your program must print to the console this

void main(){} press any key to exit..
and off course .. any other letter you add to the code (e.g. the printing code inside the main) must be printed also along with the code
Rules and Hints:
  1. the code may be in C or C++
  2. you may consider using either printf or cout to print the program any thing is fine, printf is recommended.
  3. if you can also print the #include lines.. (honestly I couldn’t).. then you are the man.
  4. “almost right” answers might be accepted as long as it RUNS and at least prints 95% of the program!
  5. Anonymous answers wont be approved, at least provide a nick name and don’t use m8 cause its mine!
  6. if you still don’t get the problem, try writing a program that prints “int main(){}” and then look at your code .. and then try to add what you added to the printed “int main(){}” and you will get the problem.
    in other words .. i need to copy the console output and place it in a cpp and run it and then take its output place it in a cpp and .. bla bla

The Shortest – Solved [Problem of the Week ]

OK, enough winners for this problem 😉

By far the most viewed topic in this blog!
and the most solved problem
I really have enjoyed noticing how every one tried to reduce it to 1 line .. to the extend that we don’t have a Second Place !

First Place 1 line (in order of who sent first)

  1. Alaa Shaker
  2. Mohammad Diab
  3. Mahmoud Osama

  4. Anonymous ! .. he didn’t leave a second name so i can’t really tell which Anonymous is he 😀 .. please send a name man .. a nickname is also fine 😀
  5. Tecnoyotta
  6. Amr Saqr
  7. Hatem
  8. Metal
  9. Mohamed Samy
  10. Mohamed Hesham – Filipino
  11. Mohamed Abdelghani
  12. Mohamed Abd-Elmone’m
  13. Roaa
  14. Tasniem Seliem

now to the solution 😀

while( *dest++ = *src++ );

we use a very unique feature .. a very dangerous feature .. its .. well i don’t know what do they call it 😀

its when an arithmetic expression can be converted implicitly to a boolean expression .. so its “implicit conversion”  i think (A)

i am sure that every one of us has spent hours and hours in debugging some code that doesn’t work as it should .. only to find out at the end that he left out some “=”..

if( x = 5 )
//instead of 
if( x == 5 )

right ? 😛

of course for old programmers its usually not an issue .. but for new comers .. well i think they still feel the sour of it 😀

but i think now that you have seen that feature being helpful sometimes .. you will never fall in that trap again.

The Shortest [Problem of the week]

Since i am extremely bored, and i have been staring into lots of code recently .. i have  no intention to do this in the blog .. so this weeks problem will be about the shortest code ever ..

ok .. go on .. i need the Shortest implementation of..



yeah.. strcpy().. our strcpy().. 😉

void strcpy(char *destination, const char *source);


  1. any language is welcome .. but C/C++ are preferred.
  2. any library functions or classes are absolutely not welcome..
  3. i don’t care for the return .. keep it void
  4. the function must take 2 arguments, one is the destination string and the other is the source string .. you can assume that both are valid (not null) pointers nor empty strings.. so forget your QA sprit for a while
  5. all strings are “null terminated char arrays”.. very normal “C strings”.
  6. input string must be kept intact, you can notice const char *
  7. read the first comment about the const char * 😀
  8. send me CODE .. and only CODE .. you can explain it , you can comment it .. but don’t send ideas alone 😀 .. try to code it 😀 .. this is the challenge.
  9. for the new faces .. welcome! 😀 .. you post the solution in the comments, and if it is right i will not approve it until it’s time, otherwise, i will approve it and say what went wrong.. that’s why you should read the comments before posting a solution.

after we are done ان شاء الله.. i will show you how to make use of a couple of features in C++ to solve this problem .. one of them is EXTREAMELY annoying feature..

OK .. That’s it .. let the race .. begin .. 😀

the solution can be found here

An Easier One [Problem of the week]

if you think last week’s problem from Alaa wasn’t that easy. 😀
and mine in the week before that was a little strange. 😀

so this week the problem will be a bit easier.. in spite of the fact that i couldn’t solve it right a way in an interview .. but i grew smart enough to solve it yesterday.. “i think its the salad 😎 ”
“some salad picture goes here .. but i couldn’t find any nice one”

How to detect a Cycle in a Linked List ??



  1. you are not Allowed to use any other data structures in you solution, no stacks no arrays no other lists .. nothing!
  2. the list is single linked .. a very normal list i mean
    and you are not allowed to change this class or add a Visited Boolean or any similar trick .. u can assume that the list Nodes are like…
    Class Node
        int val;        //assuming this is an integer List
        Node next;    //this is a pointer for those who doesnt know C#
  3. you can use any constant number of temporary variables or pointers .. as long as the number is constant (i want an in place algorithm) — i suggest 4 or 5 extra pointers
  4. you are not required to keep the list intact .. you may play with it or destroy it as you please 😀
  5. i don’t strictly require the source code .. just the idea .. but i may request it later if you insist that your answer is right 😀
  6. THOSE WHO ARE AFRAID OF BEING WRONG .. please go away .. if you are afraid of getting it wrong yo will never try .. then you will never get it right
  7. note 6 does not mean throwing me some idea and i have to think about it :@ .. you have to be 80% sure of your solution and think how may it go wrong and all the cases ..
  8. Read the comments -if there is any- before you post your solution .. it might remind you of some thing you forgot in your solution 😉

if you would like the next problem to be about some certain topic, then please tell me in the comments .. and it would be great if you said that in the very comment where you place your solution 😀

now to the winners:
thanks every one
and still no one requested a topic for the next week’s problems! .. seems like it will be very hard one (6) !

Can you C the trick ? [Problem of the week]

 Untitled How to write “X” so that:

int a= 0;
cout<<"a = "<<a;
//console says a = 5 Positive five!

a= 0;
cout<<"a = "<<a;
//console says a = -5 Negative five!

waiting for the answers in the comments (comments are moderated) .. i will show them after a couple of right answers..

if your answer is wrong “which is fine”, i will pass the comment..

if it is right .. i will reply on the comment or maybe to your mail telling you to stop trying..


now to the Winners (in order of who replied first):

  1. me 😀