Java style identifiers consist of lower and upper case letters, starting with lower cases. C++ style identifiers consist of lower case letters and the underscore sign. Given a word you should detect its style and translate it into the other style, or produce the string “Error!” if it not in one of the styles.
A linear time algorithm
It is based on a finite state automaton, that we depict here. The algorithm processes the letters of the string in order, and according to the current state possibly produces some string and moves to another state.