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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started