/ Published in: Java
charAt(#) find the char of #th therefore, you can compare char by char
Expand |
Embed | Plain Text
/* Use recursion to implement a method boolean find(String t) that tests whether a string is contained in a sentence: Sentence s = new Sentence("Mississippi"); boolean b = s.find("sip"); // return true Hint: If the text starts with the string you want to match, then you are done. If not, consider the sentence that you obtain by removing the first character http://snippets.dzone.com/posts/show/4908 */ import java.util.Scanner; public class Sentence { public int max; public int maxNumBase; public int maxNumSub; public int i; public String base; { base = word; } { int m = 1; maxNumBase = base.length(); max = maxNumBase - 1; for(i=1;i<max;i++) { if(base.charAt(i) == str.charAt(m)) { m++; if(m == 3) { return true; } } else { if(m != 0) { i--; m--; } } } return false; } } /** Base.java main here */ import java.util.Scanner; public class Base { { Sentence s = new Sentence("Mississippi"); boolean b = s.find("sip"); } }
You need to login to post a comment.
