I am asked to write a function that inputs a string and two characters, then replaces all instances of the first character with the second character; but I can only use the charAt() and length() methods from the String class. Here is what I have so far. I am hoping someone can help to point me in the right direction.
Your original method showed some misunderstanding about how Strings work. Search this website for “Strings, literally” and you get links to an old JavaRanch journal article by that name. Read that carefully. Note especially that Strings are immutable. What you did originally is pull a char out of a String, and replace it. But that has no effect on the original String. You can get the String’s contents as a char from one of its methods. You can iterate that array, replacing chars one by one as appropriate. Then you can use the array to create a new String. That may be safer than replaceAll, because you may have problems if you pass a meta‑character for regular expressions as c1 or c2.
There is also a “replace” method for Strings. That might help.
If you have been restricted to length and charAt, you can still use those methods in combination to create and populate a char.
Joined: Oct 24, 2012
@Campbell Ritchie - Thanks for the link. I will make sure to read through the journal.
@Winston Gutkowski - Thank you for pointing out the error of method usage in my posted code; it is the case that I am restricted to only the charAt() and length () methods.
The purpose of the exercise was to write a function that input a String and two characters, and then replaced all occurrences of the first character with the second character, while being restricted to only use of the charAt()/length() methods in the String class. I eventually figured out a solution to the question and realized what I was doing wrong. I was not declaring an equality statement inside the body of the if and else statements; instead, I was declaring the equality statement in the return statement.
See below for solution code, which inputs a String and two characters then replaces all instances of the first character with the second:
I appreciate everyones patience and comments!
Joined: Oct 13, 2005
That code has the major problem that you are repeatedly using the + operator in separate lines. It is efficient to use + on Strings many times in one line, but not in several lines, nor in one line called many times.
Campbell Ritchie wrote:That code has the major problem that you are repeatedly using the + operator in separate lines. It is efficient to use + on Strings many times in one line, but not in several lines, nor in one line called many times.
Of course premature optimisation is the root of all evil so you should only worry about this if you can identify this code as the cause of an unacceptable performance bottleneck. Code clarity is much more important - although appending the chars to a StringBuilder (the solution to the problem Campbell pointed out) would not reduce clarity that much.
Joined: Oct 13, 2005
An alternative would be to go back to the char. Then you can assign all the elements one by one in the loop, with or without replacements, and create only one String.
If you have a 27‑letter String, the performance overhead will be a few μs, so you probably won’t notice it. But in this sort of code, you can sit there watching it chunter away to itself…whereas this sort of thing completes in a fraction of a second:-
Campbell Ritchie wrote:An alternative would be to go back to the char...
@Zaft: And that might be the best of all, since you can easily create a String from a char (I presume there are no rules about which String constructors you can use), viz, using your own code as a basis: