Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a method called parselTongue1 that takes a String, s and an int, n and returns a String with the first ’s’ repeated n times. There is an easy solution to this task that uses theString library methodreplaceFirst-lookthisupanduseitinyourmethod.

Write a second method called parselTongue2 that does the same task, but without using replaceFirst- you will need to work out your own way to do the task using other String methods.
In both cases the output should be the same, for example:

parselTongue1("hello", 6)

would return "hello"

parselTongue1("hospital", 3) would return "hossspital"

parselTongue2("hospital", 3) would return "hossspital"

In main display the result of calling this method several times with a variety of input data.

What I have tried:

public class ParselTongue{

public static void main(String[] args){

parselTongue("Hello",6);

parselTongue("hospital",3);

}



public static void parselTongue(String s, int n){

for(char a =0; a
Posted
Updated 15-Dec-22 2:15am
Comments
Richard MacCutchan 15-Dec-22 7:51am    
There is a lot of code missing from your question. I suggest you work on writing that first.

1 solution

Remember that java strings are immutable, so you will need to copy the string up to the first "s" to a new empty StringBuilder, then add N "s"'s, then copy the remaining string characters over.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900