Is the problem that the code replaces the last "A" with an "E"?

Add an if test to see where the location is and don't replace if it is at the end.