1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Alexandra [31]
2 years ago
7

Write a method named removeDuplicates that accepts a string parameter and returns a new string with all consecutive occurrences

of the same character in the string replaced by a single occurrence of that character. For example, the call of removeDuplicates("bookkeeeeeper") should return "bokeper" .
Computers and Technology
1 answer:
Nitella [24]2 years ago
7 0

Answer:

//Method definition

//Method receives a String argument and returns a String value

public static String removeDuplicates(String str){

       //Create a new string to hold the unique characters

       String newString = "";

       

       //Create a loop to cycle through each of the characters in the

       //original string.

       for(int i=0; i<str.length(); i++){

           // For each of the cycles, using the indexOf() method,

           // check if the character at that position

           // already exists in the new string.

           if(newString.indexOf(str.charAt(i)) == -1){

               //if it does not exist, add it to the new string

               newString += str.charAt(i);

           }  //End of if statement

       }   //End of for statement

       

       return newString;   // return the new string

   }  //End of method definition

Sample Output:

removeDuplicates("bookkeeeeeper") => "bokeper"

Explanation:

The above code has been written in Java. It contains comments explaining every line of the code. Please go through the comments.

The actual lines of codes are written in bold-face to distinguish them from comments. The program has been re-written without comments as follows:

public static String removeDuplicates(String str){

       String newString = "";

       

       for(int i=0; i<str.length(); i++){

           if(newString.indexOf(str.charAt(i)) == -1){

               newString += str.charAt(i);

           }

       }

       

       return newString;

   }

From the sample output, when tested in a main application, a call to removeDuplicates("bookkeeeeeper") would return "bokeper"

You might be interested in
Write a recursive method called lengthOfLongestSubsequence(a, b) that calculates the length of the longest common subsequence (l
kompoz [17]

Answer:

Explanation:

The following code is written in Java and creates the recursive function to find the longest common substring as requested.

 static int lengthOfLongestSubsequence(String X, String Y) {

       int m = X.length();

       int n = Y.length();

       if (m == 0 || n == 0) {

           return 0;

       }

       if (X.charAt(m - 1) == Y.charAt(n - 1)) {

           return 1 + lengthOfLongestSubsequence(X, Y);

       } else {

           return Math.max(lengthOfLongestSubsequence(X, Y),

                   lengthOfLongestSubsequence(X, Y));

       }

   }

4 0
3 years ago
What is the term for an e-mail server program that receives sent messages and delivers them to their proper destination?
Katarina [22]
MTA (Mail Transfer Agent)
6 0
3 years ago
A network needs to be set up with 60 subnets. Given the IPv4 class C address 192.168.100.0, what subnet mask value should be use
d1i1m1o1n [39]
We are asked for the subnet mask and the total usable host. There are given values such as a network needs to be set up with 60 subnets given that the IPv4 class C address 192.168.100.0. The subnet mask is "255.255.255.0". The total host is "254".
6 0
3 years ago
Can someone please help me? (,:
Scorpion4ik [409]
14) Answer: Feathered images

15) Answer: Scale

Hope this helps have a nice day :)
3 0
2 years ago
Online education students need to be taught
weeeeeb [17]

Online education is a teaching practice delivered through the internet. The students learning from online platforms must be taught to be ethical.

<h3>What is the importance of ethics and integrity?</h3>

In the online education system, the students should learn to be ethical and integral as it enables the students and their teachers to be creative, free to create ideas, and acknowledge others' work.

Ethics teaches the students to develop their personalities and behavior. Students' integrity allows them to be honest, responsible, and fair towards their work and instructor.

Therefore, option A. online learning students must be taught to be ethical.

Learn more about online learning here:

brainly.com/question/14708323

#SPJ1

7 0
2 years ago
Other questions:
  • True or False<br><br> The signing of Act 26 made Cyber Harassment of a Child a crime.
    14·1 answer
  • Consider two sets S1 and S2 of size 3 and 2 each.
    13·1 answer
  • Somehow I lost 1000 points!!! Does anyone know what happened? Is it possible that I was hacked? ​
    7·2 answers
  • Wich technology can a website use to allow you to listen to a concert live over the internet?
    10·1 answer
  • 0x400C (in hexadecimal)?
    8·1 answer
  • The primary function of application software is to apply the power of the computer to enable people, workgroups, and the entire
    7·1 answer
  • __________ intelligence is an approach to boundary spanning which results from using sophisticated software to search through in
    15·1 answer
  • What would a digitizing application create?
    13·2 answers
  • The profile picture that you plan to use to market your professional brand on social media networks should feature you only.
    12·1 answer
  • Name four successful conversions to an electronic health record system in a medical facility
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!