Question: The method below takes a String parameter called str. Complete the method so that it returns a new string where each each character in the

The method below takes a String parameter called str. Complete the method so that it returns a new string where each each character in the original string has been repeated 3 times. For example if the string given is "cat", the string returned should be "cccaaattt". What needs to be added to the code below

 

 

 

 

public String tripleLetters(String str)

 

{

 

    String newString = "";

 

    // Use a for loop to traverse the characters in the string

 

    for (int i = 0; // Finish this for loop )

 

    {

 

        char letter = str.charAt(i);

 

        // perform the action in the body of the loop 3 times

        for (int j = 0; // Finish this for loop ) 

        {

 

            newString = newString + letter;

 

        }

 

    }

 

    return newString;

 

}


Step by Step Solution

3.43 Rating (159 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To complete the method so that it returns a new string where each character in the origi... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!