Don't use toCharArray() and tempOut, use String
's own length() and charAt(int) methods. That way, you don't need to copy the entire String's contents into a new char. Because that's what toCharArray() does.
Now, about the number of variables:
- input: you'll need it. That's the String to convert.
- store: you'll need it. How else can you determine when to start a new counter?
- tempOut: you can get rid of this because of my above statement.
- i: you'll need it. It's the loop counter.
- counter: you'll need it. How else can you determine how many times a character occurred?
- result: you'll need it.
So apart from tempOut you can't get rid of any of the variables.