{"record":{"id":"f6ca7719d4da52a4","repo":"TheAlgorithms/C-Sharp","slug":"invalid-character-c","errorCode":null,"errorMessage":"Invalid character {c}.","messagePattern":"Invalid character (.+?)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Stack/InfixToPostfix.cs","lineNumber":36,"sourceCode":"        /// </exception>\n        /// </summary>\n        public static string InfixToPostfixConversion(string initialInfixExpression)\n        {\n            ValidateInfix(initialInfixExpression);\n\n            Stack<char> stack = new Stack<char>();\n            StringBuilder postfixExpression = new StringBuilder();\n\n            foreach (char c in initialInfixExpression)\n            {\n                if (char.IsWhiteSpace(c))\n                {\n                    continue;\n                }\n\n                if (!IsValidCharacter(c))\n                {\n                    throw new ArgumentException($\"Invalid character {c}.\");\n                }\n\n                ProcessInfixCharacter(c, stack, postfixExpression);\n            }\n\n            EmptyRemainingStack(stack, postfixExpression);\n            return postfixExpression.ToString();\n        }\n\n        /// <summary>\n        /// <param name=\"postfixExpression\"> Postfix Expression String to Evaluate.</param>\n        /// <returns>Postfix Expression's Calculated value.</returns>\n        /// <exception cref=\"ArgumentException\">\n        /// Thrown when the input expression contains invalid characters or is null/empty.\n        /// </exception>\n        /// <exception cref=\"InvalidOperationException\">\n        /// Validates expression to have sufficient operands for performing operation.\n        /// </exception>","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Stack/InfixToPostfix.cs#L18-L54","documentation":"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.","triggerScenarios":"Calling InfixToPostfixConversion with strings containing spaces, symbols like '!', '%', '<', or braces, e.g. InfixToPostfixConversion(\"a + b ! c\").","commonSituations":"Copy-pasting expressions from textbooks/calculators with symbols such as ×, ÷, or unicode operators; untrimmed input with stray whitespace.","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."],"exampleFix":"// before\nstring postfix = converter.InfixToPostfixConversion(\"a × (b + c)\");\n// after\nstring normalized = \"a × (b + c)\".Replace('×', '*').Replace(\" \", \"\");\nstring postfix = converter.InfixToPostfixConversion(normalized);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(infix) || infix.Any(c => !char.IsLetterOrDigit(c) && !\"+-*/^()\".Contains(c))) throw new ArgumentException(\"Infix contains unsupported characters.\");","typeGuard":"bool IsValidInfixAlphabet(string s) => s.All(c => char.IsLetterOrDigit(c) || \"+-*/^()\".Contains(c));","tryCatchPattern":"try { var postfix = converter.InfixToPostfixConversion(infix); } catch (ArgumentException ex) { /* report invalid character */ }","preventionTips":["Normalize unicode operators before conversion","Trim/strip whitespace if unsupported","Validate the token alphabet against IsValidCharacter's rules up front"],"tags":["validation","stack","infix"],"backgroundTag":"invalid-argument-format","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}