✅ Need help implementing support for quoting with single quotes

Hello, I'm currently undertaking the "Build your own Shell" challenge on CodeCrafters using C#. My current task is to "Implement support for quoting with single quotes.". I have tried a few things, but have so far been unsuccessful. Here are the instructions for the task: https://pastebin.com/QEwfKAcB Here is my first attempt, along with the test it failed: https://pastebin.com/z4FkSmkQ So then, I fiddled with it a bit, but couldn't get it to work, so I told ChatGPT what was going on, which gave me this code to define parts, which passes the previous test, but fails a different one: https://pastebin.com/VTpwBHZL I don't believe this is caused by my code for running executable files, as this does work, so I believe the problem lies within the above code that ChatGPT gave me. As a result, I showed ChatGPT the issue, but it just gave me some code similar to my first attempt, which, again, didn't work. I could no longer be bothered using ChatGPT as it was just going around in circles, so I thought it would be better to get some help from real human beings. I apologise for the lengthy message, but I hope someone can help. Thanks in advance!
Pastebin
Instructions - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
1st attempt - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
2nd attempt - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
10 Replies
canton7
canton73d ago
I wouldn't use regex for this. It's a lot easier to just loop through the characters. Have a bool to say whether you're inside quotes Something like:
string input = "'/tmp/file name' '/tmp/file name with spaces'";

bool inQuotes = false;
foreach (var ch in input)
{
if (ch == '\'')
{
inQuotes = !inQuotes;
}
else if (ch == ' ' && !inQuotes)
{
// ...
}
else
{
// ...
}
}

if (inQuotes)
{
// ...
}
string input = "'/tmp/file name' '/tmp/file name with spaces'";

bool inQuotes = false;
foreach (var ch in input)
{
if (ch == '\'')
{
inQuotes = !inQuotes;
}
else if (ch == ' ' && !inQuotes)
{
// ...
}
else
{
// ...
}
}

if (inQuotes)
{
// ...
}
(That will really come in handy when you come to having to escape quotes, which is something that is really painful with regex, if you can make it work at all. Since quote escapes don't form a regular language IIRC)
I dont know
I dont knowOP3d ago
Sorry, I would’ve replied sooner but didn’t get a notification. Thanks for your answer. So, inside if (inQuotes), would I add each character to an array, then use join to make a string from it at the end of the loop?
canton7
canton73d ago
Inside the else in the loop you could add the chars to an array sure, but it would be easier to use a StringBuilder. (Or you could track the starting index of the current arg and then use Substring, but that falls down when you come to escape quotes). Inside the else if (ch == ' ' && !inQuotes), that's when you find a space that isn't in quotes, i.e. the end of one argument and the start of another. So that's where you turn the current in-progress argument into a string, probably add it to a list of string args, and clear the StringBuilder. Outside of the loop you need to check whether you're in the middle of processing an arg, and finish it if so. The if (inQuotes) means that you finished processing with mis-matched quotes, so maybe you need to raise an error
I dont know
I dont knowOP3d ago
Ahh ok, I misread the code haha. I understand now, thank you so much!
canton7
canton73d ago
Cool, good luck! (There are times when regexes are great, and there are times when breaking out a manual loop is an awful lot easier. This problem in particular is a lot easier with a manual loop) $close
MODiX
MODiX3d ago
If you have no further questions, please use /close to mark the forum thread as answered
I dont know
I dont knowOP3d ago
I may need some more help, I'm not sure yet as I'm implementing it now.
canton7
canton73d ago
Cool! I didn't mean to pressure you to close it. Feel free to open a new thread if you need
I dont know
I dont knowOP3d ago
It’s alright, it’s all working. Thanks
canton7
canton73d ago
Good to hear!

Did you find this page helpful?