It is often a good idea to write your program so that you are doing only one thing at a time. In this case, you could make a cleaned up version of the input string that is all in one case. That leaves the code you wrote before dealing with just testing for the backward-forward match.