Write a static method, getBigWords, that gets a single String parameter and returns an array whose elements are the words in the parameter that contain more than 5 letters. (A word is defined as a contiguous sequence of letters.)
LANGUAGE: JAVA
CHALLENGE:
Write a static method, getBigWords, that gets a single String parameter and returns an array whose elements are the words in the parameter that contain more than 5 letters. (A word is defined as a contiguous sequence of letters.)
EXAMPLE: So, if the String argument passed to the method was “There are 87,000,000 people in Canada”, getBigWords would return an array of two elements , “people” and “Canada”.
ANOTHER EXAMPLE: If the String argument passed to the method was “Send the request to [email protected]”, getBigWords would return an array of three elements , “request”, “support” and “turingscraft”.
SOLUTION:
public static String[] getBigWords(String sentence) { sentence = sentence.replace('@', ' '); sentence = sentence.replace('.', ' '); sentence = sentence.replaceAll("\\d+.", ""); String[] tokens = sentence.split(" "); StringBuilder sb = new StringBuilder(); for (String string : tokens) { if (string.length() > 5) { sb.append(string).append(" "); } } return sb.toString().split(" "); }