Question:
How do you define a word?
Possible answer:
Bunch of characters separated by some other characters. This second set of characters is defined by what you choose. Suppose you choose these to be . ,?;
. So if you split the input string with these characters (called delimiters), you'll get a bunch of string which are words. Now to find if the input contains the word or not, loop over these strings to check if they match your query.
Code:
boolean isPresent(String query, String s) {
String [] deli = s.split("[.\\s,?;]+");
for(int i=0;i<deli.length;i++)
if(query.equals(deli[i]))
return true;
return false;
}
tl;dr:
If you want a word to be defined as anything which consists of alphabets, numbers and underscore, there is a regex ready for you: \W+
.
String [] deli = s.split("\\W+");
Consider reading this article if you want to learn more about Java Regex.