C# Quine Problem - c#

I am trying to understand how this piece of self-replicating code works (found here), but the problem is I can't get it to run as-is:
class c {
static void Main(){
string s = "class c{{static void Main(){{string s={0}{10};System.Console.Write(s,(char)34,s);}}}}";
System.Console.Write(s,(char)34,s); //<<-- exception on this line
}
}
It's throwing an exception on writeline: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Can someone help - in particular about the formatting option {0}{10}?
I got it working like this (see below) but it's longer than the original - I am curious how the original could have worked as-is in the 1st place:
class c {
static void Main(){
string s = "class c{{static void Main(){{string s={0}{1}{2};System.Console.Write(s,(char)34,s,(char)34);}}}}";
System.Console.Write(s,(char)34,s,(char)34);
}
}

I think there is a pair of braces missing - instead of {10} it should read {1}{0}.
class c {
static void Main(){
string s = "class c{{static void Main(){{string s={0}{1}{0};System.Console.Write(s,(char)34,s);}}}}";
System.Console.Write(s,(char)34,s); //<<-- exception on this line
}
}

Could the original work with?
s={0}{1}{0}

I believe that the original was supposed to look like this:
class c {
static void Main() {
string s = "class c{{static void Main(){{string s={0}{1}{0};System.Console.Write(s,(char)34,s);}}}}";
System.Console.Write(s, (char)34, s);
}
}
I.e. the {0}{10} should just be changed to {0}{1}{0}.
The {0} in the format string is used to put the quotation marks before and after the string.

Related

How do I use a string from my Main method in another function?

In my static void Main method I have a string variable 'Name'. I want to use Name in a sub method.
//Main method holds Name
static void Main(string[] args)
{
WriteLine("Please Enter Name);
string Name = ReadLine();
}
static void subMethod()
{
// subMethod wants to use Name
foreach (char i in Name)
{
Write("~");
}
}
I thought that since both of these functions were in the same class that the sub function would automatically know what Name is since it's used in the Main method.
I've looked online and have tried different ways to reference the variable in
The closest I've gotten to be able to do this (or at least what has given me the least amount of error messages) is write this in my sub method:
//Main method holds Name
static void Main(string[] args)
{
WriteLine("Please Enter Name);
string Name = ReadLine();
}
static void subMethod()
{
//This is my attempt at referencing "Name" from Main
string Name = Main(Name);
// subMethod wants to use Name
foreach (char i in Name)
{
Write("~");
}
}
When I have this in my sub method I get these errors:
CS1503 Argument 1: cannot convert from 'string' to 'string[]'
CS0165 Use of unassigned local variable 'Name'
When I look up the error codes, it takes me to Microsoft, but everything they say about it is just a bunch of jargon that I can't understand (I'm very new to c#). It'd help a lot if anyone explained their solution as if you're talking to a 5 year old.
I can see a few mistakes in your code:
WriteLine("Please Enter Name);
You cannot just call WriteLine, you wanna tell the Console to WriteLine (or ReadLine). You are also missing quotation marks, it should be:
Console.WriteLine("Please Enter Name");
After fixing this, you can pass an argument to method like this:
static void Main(string[] args)
{
Console.WriteLine("Please Enter Name");
string Name = Console.ReadLine();
subMethod(Name);
}
static void subMethod(string name)
{
string Name = name;
//subMethod wants to use Name
foreach (char i in Name)
{
Console.Write("~");
}
}
For learning C# I would recommend watching tutorials on youtube, or reading them on some websites (f.e. here), microsoft documentation is good, but it might be a little bit too complicated for beginners.

C# Regex to replace entire function's contents

I'm having a bit of trouble getting an expression to work to replace the entire contents of a function. For example
void function1 (void)
{
Some junk here();
Other Junk here
{
Blah blah blah
}
}
I'd Like to replace the contents of this function with some predefined value ie
void function1 (void)
{
Something else here
}
This is what I have currently however it doesn't seem to work. I was trying to capture the first part of the function and then the ending curly brace which is on a new line by itself. I'm pretty new to regular expressions so forgive me if it makes no sense
text = Regex.Replace(text, #"(function1)*?(^}$))", Replace, RegexOptions.Multiline);
Any ideas what I am doing wrong or how I should go about this differently?
This is what I came up with. Let me know if it works for you.
public static string Replace_Function_Contents(string old_function, string new_contents)
{
Regex function_match = new Regex(#"(\s){1,}?([\s\w]{1,})?(\s{1,})?\(.{1,}?\)(\s{1,}){");
var match = function_match.Match(old_function);
return old_function.Remove(match.Index + match.Length) + new_contents + "}";
}
This seems to work:
/function1(?:.|\n)*?^}/m
See http://regexr.com/3geoq.
I think the major issue with your regular expression was (function1)*, which matches the string "function1" zero or more times. Example matching strings are "" and "function1function1function1". You probably meant (function1).*, but unless things work differently in .NET's regular expression engine, the . won't match newlines. I used (?:.|\n) instead to include newlines. I also dropped the captures, since your response to my question about back references didn't seem to indicate you were actually using them.
You also had an extra right parenthesis in your regular expression that I would have expected to cause an error.
Full working C# code:
using System;
using System.Text.RegularExpressions;
namespace regex
{
class Program
{
static void Main(string[] args)
{
var text = #"something up here
void anotherfunc(int x)
{
}
void function1 (void)
{
Some junk here();
Other Junk here
{
Blah blah blah
}
}
int main()
{
}";
var replacement = #"function1 (void)
Something else here
}";
Console.Out.WriteLine(Regex.Replace(text, #"function1(?:.|\n)*?^}", replacement, RegexOptions.Multiline));
}
}
}
Output:
something up here
void anotherfunc(int x)
{
}
void function1 (void)
Something else here
}
int main()
{
}

How to take a void method that takes readline and runs it though an array and assign it to a string variable

Hello I have a console application that successfully runs input through an array containing Bad Words and if there is a bad word then it will output some text then quits the application. Now I want to see what I can do With DetectBW() Including assigning it to a string Although DetectBW() is a void type and it returns nothing so assigning it to a string isn't possible if it's written like this
EX.
string Name = DetectBW();
Here's my code for further explanation
namespace CleanApp
{
class Application
{
This region contains bad words. I wrote it in otherwise it says a whole list of bad words which wouldn't be good for obvious reason.
//profanityArray()
This region contains numbers 0-318 Including 318
//ProNumArray()
This is the Main method
#region//Main() Put everything thats finished in here to output it to Console Apllication.
static void Main(string[] args)
{
string[] Sarray = profanityArray();
int[] Iarray = ProNum();
// infinite loop that writes "Ask A question." then accepts a line of text from user (DetectBW())
while (true)
{
Console.WriteLine("Ask A question.");
DetectBW();
}
}
#endregion
This is the DetectBW() method it takes PlayerInput()string Method (Console.Readline) and runs it through the profanityArray and detects if PlayerInput() contains any of the bad words in profanityArray.
#region // DetectBW()
static void DetectBW()
{
string PI = PlayerInput();
string[] PIA = profanityArray();
foreach(int i in ProNum())
{
if(PI.ToLower().Contains(PIA[i]))
{
if (PI.ToLower().Contains(" "+PIA[i]+" "))
{
Console.WriteLine(PI + " contains a bad word(s)!");
Console.WriteLine("If you can't be polite then get off!!");
Environment.Exit(0);
break;
}
if (PI.ToLower().Contains(PIA[i]+" ")|| PI.ToLower().Contains(" " + PIA[i]))
{
Console.WriteLine(PI + " contains a bad word(s)!");
Console.WriteLine("If you can't be polite then get off!!");
Environment.Exit(0);
break;
}
}
}
}
#endregion
This region is PlayerInput() it is the string method I was talking about.
#region// PlayerInput()
static string PlayerInput()
{
string S = Console.ReadLine();
return S;
}
#endregion
This is where I plan to to take the DetectBW() run it and take the same playerInput() used in DetectBW() and assign it to a string var for later use.
static void App()
{
Console.WriteLine("What is you name?");
// This is where i need Help!!
}
}
}
So this is my question:
1.
I know that I can't assign a void to a string variable but is there a way to take the same playerInput() used in DetectBW(), so I know that there isn't any bad words in it, and assign it to a name string variable.

Using if to compare strings c#

I am trying to compare strings then have it write hi if the strings are equal.
But whenever I enter AS I get nothing AS being the string i want to compare my input against.
Here is my code.
using System;
namespace testing121
{
class MainClass
{
public static void Main (string[] args)
{
long vrt;
bool run;
string pass = ("AS");
run = true;
string vrt2;
while (run)
{
if (long.TryParse (Console.ReadLine (), out vrt)) {
vrt2 = Convert.ToString (vrt);
if (String.Equals (pass, vrt2) ) {
Console.WriteLine ("Hi");
}
}
}}}}
This code just doesn't make sense. You're entering AS but then checking if it can be converted to a long as part of your condition for equality. Just do this;
public static void Main (string[] args)
{
string pass = "AS";
if (Console.ReadLine() == pass)
Console.WriteLine("hi");
}
Then, if you want to put that in a loop or whatever go for it. But I recommend starting with the simplest most basic thing. When you run this program and enter AS it will print hi
Because when you check your if
(long.TryParse
(Console.ReadLine (), out
vrt)) the result of the TryParse is always False as you don't provide a number as long.
And the console will not write you Hi.
You can also do this...
string pass = "AS";
if (pass.Equals(Console.ReadLine()))
{
Console.WriteLine("hi");
}

C# equivalent of the Ruby symbol

I'm developing a little C# application for the fun. I love this language but something disturb me ...
Is there any way to do a #define (C mode) or a symbol (ruby mode).
The ruby symbol is quite useful. It's just some name preceded by a ":" (example ":guy") every symbol is unique and can be use any where in the code.
In my case I'd like to send a flag (connect or disconnect) to a function.
What is the most elegant C# way to do that ?
Here is what i'd like to do :
BgWorker.RunWorkersAsync(:connect)
//...
private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (e.Arguement == :connect)
//Do the job
}
At this point the my favorite answer is the enum solution ;)
In your case, sending a flag can be done by using an enum...
public enum Message
{
Connect,
Disconnect
}
public void Action(Message msg)
{
switch(msg)
{
case Message.Connect:
//do connect here
break;
case Message.Disconnect:
//disconnect
break;
default:
//Fail!
break;
}
}
You could use a string constant:
public const string Guy = "guy";
In fact strings in .NET are special. If you declare two string variable with the same value they actually point to the same object:
string a = "guy";
string b = "guy";
Console.WriteLine(object.ReferenceEquals(a, b)); // prints True
C# doesn't support C-style macros, although it does still have #define. For their reasoning on this take a look at the csharp FAQ blog on msdn.
If your flag is for conditional compilation purposes, then you can still do this:
#define MY_FLAG
#if MY_FLAG
//do something
#endif
But if not, then what you're describing is a configuration option and should perhaps be stored in a class variable or config file instead of a macro.
Similar to #Darin but I often create a Defs class in my project to put all such constants so there is an easy way to access them from anywhere.
class Program
{
static void Main(string[] args)
{
string s = Defs.pi;
}
}
class Defs
{
public const int Val = 5;
public const string pi = "3.1459";
}

Categories

Resources