Question: A programmer has written a method calledreplaceLetterthat counts the amount of times a letter is present in a word. Modify this existing method to fulfill

A programmer has written a method calledreplaceLetterthat counts the amount of times a letter is present in a word. Modify this existing method to fulfill a new purpose.

Rather than count the instances of a letter in a String, a new program that replaces all instance of one letter with another. directly modifyreplaceLetterto get this program to work. In the starter code,replaceLetteronly has two parameter values. A new version should have a third parameter to indicate which String value is replacing the existing letter.

For example,

replaceLetter("hello", "l", "y") 

returns

"heyyo" 

Sample output:

Enter your word: hello Enter the letter you want to replace: l Enter the replacing letter: x hexxo 

Hint: The letters will be assigned from the user as String values. Use String methods to compare them!

public class Letter {   public static void main(String[] args)   {     // Ask the user for 3 things: their word, letter they want to replace,     // and replacing letter.         // Call the method replaceLetter and pass all 3 of these items to it for     // string processing.`   }     // Modify this method so that it will take a third parameter from a user that is the String they want to   //to replace letterToReplace with. This method should return the modified String.   public static int replaceLetter(String word, String letterToReplace)   {         int count = 0;     for(int i = 0; i < word.length(); i++)     {       if(word.substring(i, i+1).equals(letterToReplace))       {         count++;       }     }     return count;   } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!