C# Infix to Prefix Converter
<p>I writing a small application that converts infix to postfix and prefix.  The conversion to postfix has been accomplished but I am having difficulties with converting to prefix.</p>

<p><strong>This is what I have:</strong></p>

<pre><code>public class PrefixConverter {
    string[] operators = { "+", "-", "/", "*", "^" };

    public string toPrefix(string infix) {
        StringBuilder sb = new StringBuilder(infix);
        int index = sb.Length;
        string operand = "";
        int steps = 0;
        for (int i = 0; i &lt; sb.Length; i++) {
            for (int x = 0; x &lt; operators.Length; x++) {
                int tempIndex = IndexOf(sb,operators[x], i,false);
                if (tempIndex &lt; index &amp;&amp; tempIndex &gt;= 0) {
                    index = tempIndex;
                    operand = operators[x];
                }
            }
            if (index == sb.Length) {
                break;
            }
            sb.Remove(index, index + operand.Length + 1); //Throws exception
            for (int x = index - 1; x &gt;= 0; x--) {
                if (sb[x] == '(' &amp;&amp; steps == 0) {
                    sb.Insert(x, operand);
                    break;
                } else if (sb[x] == '(') {
                    steps--;
                } else if (sb[x] == ')') {
                    steps++;
                }
            }
            i = index;
            index = sb.Length;
        }
        return sb.ToString();
    }
    private int IndexOf(StringBuilder sb, string value, int startIndex, bool ignoreCase) {
        int index;
        int length = value.Length;
        int maxSearchLength = (sb.Length - length) + 1;

        if (ignoreCase) {
            for (int i = startIndex; i &lt; maxSearchLength; ++i) {
                if (Char.ToLower(sb[i]) == Char.ToLower(value[0])) {
                    index = 1;
                    while ((index &lt; length) &amp;&amp; (Char.ToLower(sb[i + index]) == Char.ToLower(value[index])))
                        ++index;

                    if (index == length)
                        return i;
                }
            }

            return -1;
        }

        for (int i = startIndex; i &lt; maxSearchLength; ++i) {
            if (sb[i] == value[0]) {
                index = 1;
                while ((index &lt; length) &amp;&amp; (sb[i + index] == value[index]))
                    ++index;

                if (index == length)
                    return i;
            }
        }
        return -1;
    }
}
</code></pre>

<p>I am not a big fan of this implementation (plus it doesn't work), if anyone has anything better that you can share that would be great.</p>
