TheAlgorithms/C-Sharp · error · ArgumentException

Invalid character .

Error message

Invalid character {c}.

What it means

InfixToPostfixConversion throws ArgumentException when the infix string contains a character rejected by IsValidCharacter (not a letter, digit, operator, or parenthesis). The converter only supports a fixed token alphabet and aborts instead of guessing.

Solutions

  1. Normalize the expression first: replace unicode operators (×, ÷) with *, / and remove unsupported symbols.
  2. Trim whitespace or strip spaces before conversion if whitespace is rejected by IsValidCharacter.
  3. Catch ArgumentException and surface it as user input validation before calling the converter.

Example fix

// before
string postfix = converter.InfixToPostfixConversion("a × (b + c)");
// after
string normalized = "a × (b + c)".Replace('×', '*').Replace(" ", "");
string postfix = converter.InfixToPostfixConversion(normalized);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(infix) || infix.Any(c => !char.IsLetterOrDigit(c) && !"+-*/^()".Contains(c))) throw new ArgumentException("Infix contains unsupported characters.");

Type guard

bool IsValidInfixAlphabet(string s) => s.All(c => char.IsLetterOrDigit(c) || "+-*/^()".Contains(c));

Try / catch

try { var postfix = converter.InfixToPostfixConversion(infix); } catch (ArgumentException ex) { /* report invalid character */ }

Prevention

When it happens

Trigger: Calling InfixToPostfixConversion with strings containing spaces, symbols like '!', '%', '<', or braces, e.g. InfixToPostfixConversion("a + b ! c").

Common situations: Copy-pasting expressions from textbooks/calculators with symbols such as ×, ÷, or unicode operators; untrimmed input with stray whitespace.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/f6ca7719d4da52a4. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Stack/InfixToPostfix.cs:36

        /// </exception>
        /// </summary>
        public static string InfixToPostfixConversion(string initialInfixExpression)
        {
            ValidateInfix(initialInfixExpression);

            Stack<char> stack = new Stack<char>();
            StringBuilder postfixExpression = new StringBuilder();

            foreach (char c in initialInfixExpression)
            {
                if (char.IsWhiteSpace(c))
                {
                    continue;
                }

                if (!IsValidCharacter(c))
                {
                    throw new ArgumentException($"Invalid character {c}.");
                }

                ProcessInfixCharacter(c, stack, postfixExpression);
            }

            EmptyRemainingStack(stack, postfixExpression);
            return postfixExpression.ToString();
        }

        /// <summary>
        /// <param name="postfixExpression"> Postfix Expression String to Evaluate.</param>
        /// <returns>Postfix Expression's Calculated value.</returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the input expression contains invalid characters or is null/empty.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Validates expression to have sufficient operands for performing operation.
        /// </exception>

View on GitHub (pinned to 96e2905cab)