I want to change the first method to a while and for loop. I have added the code below. Is this correct?
public String extractWordFour(String text){
if(text.length()==0 || text.charAt(0) == ''){
return "";
} else {
return text.charAt(0) + extractWordFour(text.substring(1));
}
}
public String extractWordFour(String text){
int i=0;
while (i<=text.length){
return text.charAt(0) + extractWordFour(text.substring(i));
i++;
}
}
public String extractWordFour(String text){
for(int i=0;i<=text.length();i++){
return text.charAt(0) + text.substring(1);
}
}
I’m not going to answer this question because I think its a h.w. assignment but I’m putting it in an answer since I can’t fit this in a comment, but I’m assuming you want to convert a recursive solution into a while or for loop solution.
Your while solution is wrong first of all because you are mixing recursion and while together. You should not be calling the function inside your while loop!
This one can’t finish because you are returning it before its even looped more than once.
Anyways hope that helps. The best thing to do would be to fully understand what the first function does, write it down and then think about how to make it do the exact same thing with a while or a for loop.