0

I've written a simple program for finding the smallest permutation of a string which is lexicographically larger than the current one. But, the compiler emits the error

ERROR CS1003 Syntax error, ':' expected* ".

I use VS 2015 (update 3) and whenever I compile this program (which seems to be grammatically true), I encounter the aforementioned error.

Does this program have any error in syntax?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        const string a = "ABCDEFG";
        //var u = FFG(a);
        //var t = int.Parse(Console.ReadLine());
        //for (int i = 0; i < t; i++)
        //{
        //    Console.WriteLine(FFG(Console.ReadLine()));
        //}
        string u2 = a;
        string u = a;
        do
        {
            //***The follownig line meets Error***
            Console.WriteLine(u + $"{String.Compare(u, u2) > 0 ? true:false}");

        } while ((u = FFG(u)) != "no answer");
        Console.ReadLine();
    }
    static string FFG(string ss)
    {
        var s = ss.ToCharArray();
        int i = s.Length - 1;
        while (i >= 1 && s[i] <= s[(i--) - 1])
        { }
        if (i == 0 && s[0] >= s[1])
            return "no answer";
        int j = s.Length - 1;
        while (s[i] >= s[(j--)])
        { }
        j++;
        swap(s, i, j);
        int t = i + 1, tt = s.Length - 1;
        if (j - i >= 2)
            while (t < tt)
            {
                //if (t == j)
                //    t++;
                //if (tt == j)
                //    tt--;
                swap(s, t, tt);
                t++; tt--;
            }
        return new string(s);
    }
    static void swap<T>(T[] array, int i, int j)
    {
        T k = array[i];
        array[i] = array[j]; array[j] = k;
    }
}
0

2 Answers 2

2

This is not a compiler bug. The problem is the interpolated string format you have:

$"{String.Compare(u, u2) > 0 ? true:false}"

Conditional expressions are required to be surrounded by parentheses because of the : within. Normally, the : is followed by a formatting expression, which in this case there is none.

Try using this instead:

$"{(String.Compare(u, u2) > 0 ? true:false)}"

From MSDN (emphasis mine):

You do not need to quote the quotation characters within the contained interpolation expressions because interpolated string expressions start with $, and the compiler scans the contained interpolation expressions as balanced text until it finds a comma, colon, or close curly brace. For the same reasons, the last example uses parentheses to allow the conditional expression (p.Age == 1 ? "" : "s") to be inside the interpolation expression without the colon starting a format specification.

For what it's worth, in my opinion, using an interpolated string here is unnecessary. The result of your string comparison could be useful for debugging, so I would move that out of the string. Additionally, Console.WriteLine already is overloaded to accept a format string and tokens, so I would change your console output logic to:

do 
{
    var comparisonResult = string.Compare(u, u2) > 0;
    Console.WriteLine("{0} {1}", u, comparisonResult);
} ...

You can .ToLower() as well if you really care about the casing.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the gentle answer to a classic 'it must be a compiler bug' question. It would have been easy to be snarky as well as correct and thorough. Instead you were kind. Good job!
Yes, there are much more options to choose such as one you mentioned (and even more brief ones) ; but what amazed me was "why this particular form of Interpolated Strings doesn't work ?" , which you concisely stopped my amazement ! Thanks
Funnily enough, you have to think like a compiler to know why this wasn't a compiler bug :) Glad I could help.
0

I think Cᴏʀʏ already answered your question. But in case want to you a different one.

Try String.Format:

Console.WriteLine("{0}{1}", u, String.Compare(u, u2) > 0 ? true : false);

Hope it helps!

1 Comment

Oh, didn't know that. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.