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
- Normalize the expression first: replace unicode operators (×, ÷) with *, / and remove unsupported symbols.
- Trim whitespace or strip spaces before conversion if whitespace is rejected by IsValidCharacter.
- 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
- Normalize unicode operators before conversion
- Trim/strip whitespace if unsupported
- Validate the token alphabet against IsValidCharacter's rules up front
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.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Invalid character ' ' found in the expression.
- Mismatched parentheses in expression.
- Mismatched parentheses.
- message
- key must be non-empty string
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)