Complete the seperateDuplicateChars() method in SeperateDuplicates.java as follows: - The method takes a String str as a parameter and returns a new String. - The returned String should be exactly like str, but any identical characters that appear in a consecutive way must be separated by hyphens "-". - You may ONLY use the following methods from the String class: charAt(), substring() and length(). Other than uncommenting the code, do not modify the main method in Seperate Duplicates.java. Sample runs provided below. Argument String str "Hello" "Bookkeeper" "Yellowwood door" "Chicago Cubs" Return String "Hel-lo" "Bo-ok-ke-eper" "Yel-low-wo-od do-or" "Chicago Cubs"

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter3: Using Methods, Classes, And Objects
Section: Chapter Questions
Problem 1GZ
icon
Related questions
Question
public class SeperateDuplicates { public static void main(String[ args) { System.out.printIn(seperateDuplicatesChars("Hello")); System.out.printin(seperateDuplicatesChars ("Bookkeeper"')); System.out.printin(seperateDuplicatesChars("Yellowwood door"')); System.out.printin(seperateDuplicatesChars("Chicago Cubs")); */ public static String seperateDuplicatesChars(String str) { //To be completed } }
Complete the seperateDuplicateChars () method in SeperateDuplicates.java as
follows:
- The method takes a String str as a parameter and returns a new String.
The returned String should be exactly like str, but any identical characters
that appear in a consecutive way must be separated by hyphens "-".
You may ONLY use the following methods from the String class: charAt(),
substring() and length().
Other than uncommenting the code, do not modify the main method in
SeperateDuplicates.java.
Sample runs provided below.
Argument String str
"Hello"
"Bookkeeper"
"Yellowwood door"
"Chicago Cubs"
Return String
"Hel-lo"
"Bo-ok-ke-eper"
"Yel-low-wo-od do-or"
"Chicago Cubs"
Transcribed Image Text:Complete the seperateDuplicateChars () method in SeperateDuplicates.java as follows: - The method takes a String str as a parameter and returns a new String. The returned String should be exactly like str, but any identical characters that appear in a consecutive way must be separated by hyphens "-". You may ONLY use the following methods from the String class: charAt(), substring() and length(). Other than uncommenting the code, do not modify the main method in SeperateDuplicates.java. Sample runs provided below. Argument String str "Hello" "Bookkeeper" "Yellowwood door" "Chicago Cubs" Return String "Hel-lo" "Bo-ok-ke-eper" "Yel-low-wo-od do-or" "Chicago Cubs"
Expert Solution
Step 1: Method 1

Here's the implementation for  seperateDuplicatesChars() method:

CODE: 

public static String seperateDuplicatesChars(String str) {
    StringBuilder result = new StringBuilder();
    result.append(str.charAt(0));
    for (int i = 1; i < str.length(); i++) {
        if (str.charAt(i) != str.charAt(i - 1)) {
            result.append(str.charAt(i));
        } else {
            result.append("-").append(str.charAt(i));
        }
    }
    return result.toString();
}

 

 

 

OR:

OR:

 

 

 

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Generic Type
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT