#QJ0 : c# : parsing string as expected still test case failing

`

static List HandleQuotes(string input)
{
var args = new List();
var current = new System.Text.StringBuilder();

   bool inSingleQuote = false;
   bool inDoubleQuote = false;

   for (int i = 0; i < input.Length; i++)
   {
       char c = input\[i\];

       if (c == '\\\\')
       {
           if (!inSingleQuote && !inDoubleQuote)
           {
               if (i + 1 < input.Length)
               {
                   current.Append(input\[i + 1\]);
                   i++;
               }
               continue;
           }

           if (inDoubleQuote)
           {
               if (i + 1 < input.Length &&
                   (input\[i + 1\] == '"' || input\[i + 1\] == '\\\\' || input\[i + 1\] == '\\''))
               {
                   current.Append(input\[i + 1\]);
                   i++;
               }
               else
               {
                   current.Append('\\\\');
               }
               continue;
           }
           current.Append('\\\\');
           continue;
       }

       if (c == '\\'' && !inDoubleQuote)
       {
           inSingleQuote = !inSingleQuote;
           continue;
       }

       if (c == '"' && !inSingleQuote)
       {
           inDoubleQuote = !inDoubleQuote;
           continue;
       }

       if (char.IsWhiteSpace(c) && !inSingleQuote && !inDoubleQuote)
       {
           if (current.Length > 0)
           {
               args.Add(current.ToString());
               current.Clear();
           }
       }
       else
       {
           current.Append(c);
       }
   }

   if (current.Length > 0)
       args.Add(current.ToString());

   return args;

}`

Hi, thanks for your post!

I’m currently out of the office for the holidays and will be back on January 5. I’ll get back to you as soon as possible once I return.

Your paste looks misformatted. I’m assuming in your code paste that char c == ‘\\\\’ is actually char c == ‘\\’ so actually means you are comparing c with the character \ itself, and that input\[i + 1\]` is actually input[i + 1].

Your program is not finding the command. I suspect it might be because you are “unescaping” the \’given as input. You need to run exe with \’single quotes'\’ but you are running exe with ‘single quotes’.

Your logic at least is consistent with this explanation. Your have inDoubleQuote == true when you reach the \’ part. You then check the second char of that and if it is ' then you only append that to the current token. But that is wrong: "\'" is supposed to be \’ and not just '.

Edit: see previous stage:

Within double quotes, a backslash only escapes certain special characters: ", \, $, `, and newline. For all other characters, the backslash is treated literally.

1 Like

Thank you its passing now

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.