0

The question is pretty self explanatory...Design a method called startChar(String str, char c). This is a code i found here but it insert char at the end of the String. I am at a loss to think recursively. I understand this code but dont understand it enough to manipulate it to place chars at the start. Help of any kind is appreciated.

Example Input:

startChar("Apple",'p')

Output:

ppale

The code

public static String chrToLast(String str, char ch) {
    //This if statement details the end condition
    if(str.length() < 1) {
        return "";
    }


     String newString = str.substring(1); //Create new string without first 
character

    if(str.indexOf(ch) == 0) { //This happens when your character is found
        return chrToLast(newString, ch) + ch;
    } else { //This happens with all other characters
        return str.charAt(0) + chrToLast(newString, ch);
    }
}
5
  • I don't understand what this code is intended to do; can you provide additional examples? Why is the output "ppale"? Commented Jul 15, 2018 at 23:39
  • I'd expect it to return Alepp. Isn't the idea to move all instances of the character to the end? So the p characters both come out of their original positions, and go to the end. Commented Jul 15, 2018 at 23:45
  • Oh, I see. You want to reverse it so it moves characters to the start instead of the end? So the expected output would be ppAle, right? Commented Jul 15, 2018 at 23:47
  • Yes! For example consider input: (banana, a) then the output should be aaabnn. Take all the a's , remove them and put them at the start of the string. Commented Jul 16, 2018 at 0:16
  • Please add all explanations and examples to the post, not as comments. Commented Jul 16, 2018 at 7:13

1 Answer 1

1

What about...

public static void main(String[] args) {
    String s = startChar("Apple", 'p');
    System.out.println("");
}

public static String startChar(String str, char ch) {
    return startChar(str,ch,"","");
}

private static String startChar(String str, char ch, String acc, String chs) {
    //This if statement details the end condition
    if (str.length() < 1) {
        return chs + acc;
    }


    String newString = str.substring(1); //Create new string without first character

    if(str.charAt(0) == ch) { //This happens when your character is found
        return startChar(newString, ch,acc, chs + ch);
    } else { //This happens with all other characters
        return startChar(newString, ch,acc+str.charAt(0), chs);
    }
}

This is recursive with a auxiliary function

UPDATE: you must know/remember that you can procesate your data before and after the recursive call, but try to write your recursive call at the end, generally most languages has optimization in that case.

In this example we use an accumulator to accumulate processed data, then in the base step we processed those accumulator to the final output.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent solution. I have so much difficulty grasping recursion and you seem to come up with that solution on a whim. I think my fault is thinking through trial and error which never works. Although true, it is using an auxiliary function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.