Created
October 9, 2012 16:24
-
-
Save ebot/3859852 to your computer and use it in GitHub Desktop.
Samples for Donita
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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