X1637: hasPrefix(word)?

Write the method boolean hasPrefix(String word) in Java that returns true if the string in word begins with one of these prefixes:

"pre", "post", "mid", or "un"

If the beginning of the word has one of those prefixes, then return true, otherwise return false.

Note that you cannot use startsWith() to do the comparison.

You may consult the Java String class to help you solve this problem.

Examples:

hasPrefix("preview") -> true
hasPrefix("midpoint") -> true
hasPrefix("computer") -> false

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.