Studyon Minte9.com
JUnit




Function test



	%$java
		package testexample;
		import org.junit.Assert.*;
		import junit.framework.*;

		public class Testexample extends TestCase {
		    
		    public static void main(String[] args) {
			assertEquals(1, last2("hixxhi"));
			assertEquals(1, last2("xaxxaxaxx"));
			assertEquals(2, last2("axxxaaxx"));
			assertEquals(2, last2("xxxx"));
		    }
		    
		    /**
		     * Find 2 end substring, 
		     * then count total times when the substring appear,
		     * exclude the end
		     * @param str
		     * @return int
		     */
		    public static int last2(String str) { 
			
			if (str.length() < 2) return 0;
			
			String end = str.substring(str.length() - 2);
			
			int count = 0;
			
			for (int i=0; i<str.length()-2; i++) { // don't count last

			    if (str.substring(i, i+2).equals(end)) {
				count++;
			    }
			}
			return count;
		    }
		}