Tuesday, November 6, 2012

Java Exercise #44: Checking Substring

Java provides indexOf method in the String class to check whether a String is a sub-string of another string. The problem is to implement such a function.

This implementation helped me to come to know about labeled break and continue in Java. Default break/continue (non-labeled) breaks or continue from the inner most for, switch, while, do-while statements. However labeled ones help to break from/continue with the outer statements.

Exercise: Write a program that prompts the user to enter two Strings and then verify whether the first String is a sub-string of the second.


Enter the first string: el
Enter the second string: hello
el is a substring of hello

Enter the first string: hello
Enter the second string: al
No substring found



Download Code: Check Sub-String

1 comment: