171k views
2 votes
Write a method named stutter that accepts a string parameter returns a new string replacing each of its characters with two consecutive copies of that character. For example, a call of stutter("hello") would return "hheelllloo".

User Darkseal
by
3.4k points

1 Answer

3 votes

Answer:

public static String stutter(String a ) {

String myText = "";

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

myText = myText + a.charAt(i) + a.charAt(i);

}

return myText;

}

Step-by-step explanation:

Programming language used: Java

public static String stutter(String a )

//Define a method stutter which is public (accessible from outside of the class) and static(accessible without creating the object of its class) and with a string argument

String myText = "";

//Defines an empty string

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

myText = myText + a.charAt(i) + a.charAt(i);

}

//loop through each character of the string and concatenate each character twice

return myText;

//return the final string

User Sky
by
3.2k points