Skip to content

Instantly share code, notes, and snippets.

@ebot
Created October 9, 2012 16:24
Show Gist options
  • Save ebot/3859852 to your computer and use it in GitHub Desktop.
Save ebot/3859852 to your computer and use it in GitHub Desktop.
Samples for Donita
public class Recursion {
private String sentence;
public Recursion(String sentence) {
this.sentence = sentence;
}
public String getSentence() {
return sentence;
}
public boolean find(String search) {
return find(search, sentence);
}
private boolean find(String search, String phrase) {
if (phrase.startsWith(search)) {
System.out.println(" " + search + " found in " + phrase + ".");
return true;
}
else if (phrase.length() < search.length()) {
return false;
}
else {
String newPhrase = phrase.substring(1);
System.out.println(" There are "
+ newPhrase.length()
+ " character(s) left to check.");
return find(search, newPhrase);
}
}
public static void main (String [] args) {
Recursion r = new Recursion("I swam in the Mississippi river yesterday.");
System.out.println("Searching for sip in " + r.getSentence() + "...");
System.out.println("Done - " + new Boolean(r.find("sip")).toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment