Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Given the following C# program outputting False, inject a 'malicious' line of code such that the program outputs True.

class Program
{
    static void Main()
    {
        System.Console.Write("False");
        ;
    }
}

Your answer should consist of a string that replaces the second semicolon, and causes the program to output True, the whole True and nothing but True (not even a newline). It must do this if stdout is printed to the console, and if stdout is redirected to a file.

The shortest answer wins.

share|improve this question
    
What's your objective winning criterion? –  J B Jan 3 at 14:14
    
@JB Do puzzles need a winning criterion? Will edit. –  Kendall Frey Jan 3 at 14:15
    
I'm mostly reacting to the code-golf remark, but the point stands: which will be the accepted answer? –  J B Jan 3 at 14:16
    
@JB Shortest answer is best answer. –  Kendall Frey Jan 3 at 14:16
    
Well it is code-golf, then, isn't it? –  J B Jan 3 at 14:18

5 Answers 5

up vote 17 down vote accepted

52 characters

}static Program(){System.Console.Write(0<1);for(;;);

so the whole thing becomes:

class Program
{
    static void Main()
    {
        System.Console.Write( "False" );
    }
    static Program()
    {
        System.Console.Write( 0 < 1 ); 
        for ( ; ; ) ;
    }
}
share|improve this answer
    
Sorry for editing the question in the middle of your answer. Newlines are no longer allowed, you'll have to use Write. –  Kendall Frey Jan 3 at 15:16
    
Ok, I've changed WriteLine to Write –  Danko Durbić Jan 3 at 15:18
1  
You could probably save some bytes by replacing your exit with for(;;);, nothing I've seen requires that the program terminates...? –  Joachim Isaksson Jan 3 at 16:19
    
@JoachimIsaksson Yes, that meets the requirements as specified. –  Kendall Frey Jan 3 at 16:28
2  
}static Program(){System.Console.Write(0<1);for(;;); could always become }static Program(){for(System.Console.Write(0<1);;); –  NPSF3000 Feb 18 at 5:33

C#, 51 characters

Console.Write("\b\b\b\b\b");Console.Write("True ");

Will only work on standard output

share|improve this answer
    
First thing I tried, unfortunately it won't work because of the initial WriteLine. –  Vereos Jan 3 at 15:09
3  
Now it is Write :/ I don't get how we would manage to solve this if it keeps changing... –  Vereos Jan 3 at 15:12
    
@Vereos I wanted to remove the ambiguity of 'newline or no newline'. –  Kendall Frey Jan 3 at 15:13
3  
You don't need 2 instances of Console.Write, and you could use \r instead of 5 \bs. Console.Write("\rTrue "); –  Brian S Jan 3 at 21:10
    
I don't think it meets the criteria. It actually outputs False first, which is then overwtitten by True later. –  microbian Feb 4 at 7:44

You can generalize RamonBoza's answer to still work if stdout is redirected to a file.

Assuming the program runs under Mono on Linux, with the assembly Mono.Posix.dll loaded:

if(Mono.Unix.Native.Syscall.isatty(1))
{
    Console.Write("\rTrue ");
}
else
{
    // Truncate the output file first
    Console.OpenStandardOutput().SetLength(0);
    Console.Write("True");
}

I doubt this works on Windows, but there's probably an equivalent to isatty().

share|improve this answer

83... WIP

I was hoping this'd work, but apparently "False" isn't being interned as I'd hoped in my compiler

//}unsafe static Program(){fixed(char*f="False"){*(long*)f=0x65007500720054;*(f+4)=' ';}
}unsafe static Program(){fixed(char*f="False")for(int i=5;i-->0;)*(f+i)="True "[i];

ungolfed

class Program
{
    static void Main(string[] args)
    {
        Console.Write("False");
    }

    unsafe static Program()
    {
        fixed (char* f = "False") for (int i = 5; i-- > 0; ) *(f + i) = "True "[i];
    }
}
share|improve this answer
class Program
{
    static void Main()
    {
        System.Console.Write("False");

        System.Console.Clear(); System.Console.Write("True");

        Console.ReadLine();
    }
}
share|improve this answer
    
This won't work if stdout is redirected. –  Kendall Frey Feb 19 at 13:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.