How to print a string letter by letter, adding a star between each character, without a loop (recursively)?

here is the function:

public static String allStar(String a) {

I’ve equally done:

int n = a.length(); // to get length of the string to use somehow.
                    // But I'm not sure how to actually apply it. 

I’ve also used the charAt() function however this would probably only work with a loop.

Please help explain how to do it


Beshambher’s answer is a neat and easy way to do it. But if you want to do it recursively then :

public static String allStar(String a) {
    if(a == null || a.isEmpty()) {        
        return "";

    if(a.length() == 1) {
        return a;
    return  a.charAt(0) + "*" + allStar(a.substring(1));

Leave a Reply

Your email address will not be published. Required fields are marked *