Write a while loop that replaces every occurrence of “cat” in the message with “dog” using the indexOf and substring methods.

Given:
public class ChallengeReplace
{
public static void main(String[] args)
{
String message = "I love cats! I have a cat named Coco. My cat's very smart!";

// Write a loop here that replaces every occurrence of "cat"
// in the message with "dog", using indexOf and substring.






}
}

Answers

Answer 1

Answer:

Complete the program using:

int index = message.indexOf("cat");

while(index != -1){

message = message.replace("cat","dog");

index = message.indexOf("cat");

}

System.out.print(message);

}

}

Explanation:

The line gets the index of string "cat"

int index = message.indexOf("cat");

The following operation is repeated until all occurrence of "cat"s has been replaced with dog

while(index != -1){

message = message.replace("cat","dog");

index = message.indexOf("cat");

}

This prints the new string

System.out.print(message);

}

}

Answer 2

The program illustrates the use of loops.

Loops are used to perform repetitive and iterative operations.

The code segment where comments are used to explain each line, is as follows:

//This gets the index of "cat" in the string message

int index = message.indexOf("cat");

//The following loop is repeated, until there is no occurrence of "cat" in the string

while(index != -1){

//This replaces "cat" with "dog"

message = message.replace("cat","dog");

//This gets another index of "cat" in the string message

index = message.indexOf("cat");

}

//This prints the new string

System.out.print(message);

}

}

At the end of the loop, all occurrence of "cat" are replaced with "dog".

Read more about similar programs at:

https://brainly.com/question/20461017


Related Questions

Which of the following might cause a mobility impairment that limits computer use? Select 3 options.
a color blindness
Odyslexia
O neurological issue
injury
O genetic issue

Answers

Explanation:

A color blindness , injury .

Answer:

neurological issueinjurygenetic issue

Explanation:

The question is about mobility impairment. Color blindness and dyslexia have to do with vision, so rarely cause mobility issues related to computer use. Rather they would cause issues related to reading the screen.

The remaining three choices are the ones you want.


What is a goal?
What is a strategy?
What are tactics?
hy is it important for a goal to be specific?

Answers

A goal is an aim you want to achieve
Strategy is a plan to your achievement
Tactics are is an action carefully planned
A goal is important to be specific cuz it helps u focus on your specific task

which are the two alternatives for pasting copied data in a target cell groups

Answers

Answer:

Excel has decided to paste contents into a single cell, rather than keeping data in columns. When copying from a query in SQL that has data in a table, pasting it into Excel would normally split the data into the columns - keeping the table. But Excel has automatically changed and takes out the table, pasting each row into one cell.

Explanation:

100 POINTS PLEASE HELP Ken Thompson and Dennis Ritchie co-created which operating system?

Answers

Answer:

UNIX operating system

Explanation:

Unix is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, development starting in the 1970s at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and others.

Answer:

the UNIX

During the 1960s to 1970s, Bell Labs colleagues Ken Thompson and Dennis Ritchie developed UNIX, a multi-tasking, multi-user operating system alternative to the batch processing systems then dominating the computer industry.

Explanation:

The way things are arranged on a publication is referred to as the _____.

style
guides
layout
scheme

Answers

Answer: I’m pretty sure it’s layout

For your biology class, you will be giving a presentation of the findings of a plant growth experiment. Which application is best suited for this presentation?


Writer or Word


Impress or PowerPoint


Notepad or Paint


Calc or Excel

Answers

Answer: Powerpoint

Explanation: Easy to give graphs and add pictures. You can also add shapes to create a bar graph if needed.

Answer:

Calc or Excel

Explanation:

The ____, which amplified weak electrical signals, enabled electrical sound recording.

Answers

Answer:

Radio

Explanation:

Bill has several workbooks open, and he would like to see them all side by side on the screen. Which option on the
Arrange All menu should he use?
Tiled
Cascade
Horizontal
Vertical

Answers

Answer:

tiled

Explanation:

A variety of fonts may be used in letter writing as long as the font size is 12 points.
True
False

Answers

False MLA format is Times New Roman font, size 12, double spaced.

Answer:

False

Explanation:

Which sentences describe the value of a conditional statement? Check all that apply.

It allows a programmer to designate a portion of the code that will be run only when a condition is met.

It creates new program code when a condition is met.

It allows a programmer to set a condition that must be met.

It incorporates decision-making into a program.

Answers

Answer:

134

Explanation:

Edgenuity

Answer:

1. It allows a programmer to designate a portion of the code that will be run only when a condition is met.

3. It allows a programmer to set a condition that must be met.

4. It incorporates decision-making into a  program.

Explanation:

Other Questions
If 25.98 mL of 0.1180 M KOH reacts with 52.50 mL of CH3COOH solution, what is the molarity of the acid solution? CivilizatiorWhich of the following was a reason many in Southeast Asia converted to Islam?A) converts were offered social advantagesB) governments threatened military action against those who did not. C) citizens were banned from all political activity if they did not convert.D) converts were offered forgiveness of all debts. HELP!!!!!!!!!!!!!!!!!! Which choice has prokaryotic cells?Lactobacillusbeet rootrose thornhuman kidneyPLEASE HELP! somebody help please, and give the steps so I can learn how to solve it myself According to lesson and lesson 8 Do sequencing matter true or false Solid diarsenic trioxide reacts with fluorine gas (F2) to produce liquid arsenic pentafluoride and oxygen gas (O2). Write the Qc for this reaction. Please choose the best description, or definition of Faunal Succession.principle stating that specific groups of organisms lived and died within a specifictime frame due to the ice ageprinciple stating that specific groups of fawns lived in a specific region and arefound in the rocks shoing fawns onl live in specific areas.principle stating that specific layers of the earth have contain fauna of differenttypes showing biodiversity.rinciple stating that specific groups of organisms have followed, or succeeded.one another in a definite sequence through Earth history 15 tens 7 ones what is the answer? Which type of color is created by mixing two primary colors? Its in the picture help me out plsss What is the rule of law and why is this idea important? How many solutions does this have? 2x = 2x - 9A. NoB. OneC. TwoD. Infinity Which relationships have the same constant of proportionality between yyy and xxx as the equation y=\dfrac{1}{2}xy= 2 1 xy, equals, start fraction, 1, divided by, 2, end fraction, x? Choose 3 answers: Choose 3 answers: The ratio of boys to girls in the schools' math team is five to six. Which combination of boys to girls could the team have?ten boys to twelve girlsthirty boys to twenty five girlstwenty five boys to thirty girlseighteen boys to fifteen girlstwelve boys to ten girlsfifteen boys to eighteen girls whats is the answer to this ? what is the force produced on a body of 30kg mass when a body moving with the velocity of 26km/hr is acceleted to gain the velocity of 54 km/hr in 4 sec Question 10 (1 point)In what ways were the Byzantines culturally distinct from the Western RomanEmpire?The Romans were Christian and spoke Latin.The Byzantines were Christian and spoke Latin.The Byzantines were Christian and spoke Greek.The Romans were Christian and spoke Greek. Which of the following might cause a mobility impairment that limits computer use? Select 3 options.a color blindnessOdyslexiaO neurological issueinjuryO genetic issue A dictator junk gang has all the gas left in the region. They are requiring 2 kg of scrapmetal in exchange for gasoline which they are selling by the liter. Your water rig isAmerican made with a gallon gas tank and it can drive 30 miles per gallon. How manyliters of gasoline do you need from the junk gang in order to successfully make it thenew destination?