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.)

2
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 support@turingscraft.com”, 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(" ");
}	

2 comments

  • j p says:

    Use this:

    public static String[] getBigWords(String str) {
    str = str.replace(“@”, ” “);
    str = str.replace(“.”, ” “);
    str = str.replace(“,”, “”);
    str = str.replace(“\\d+”, “”);
    String[] myArr = str.split(” “);
    java.util.ArrayList al = new java.util.ArrayList ();
    for (int i = 0; i < myArr.length; i++) {
    boolean notLetter = false;
    for (int j = 0; j 5) {
    al.add(myArr[i]);
    }
    }
    String[] arr = al.toArray(new String[0]);
    return arr;
    }

  • shusma kafle says:

    this workspublic static String[] getBigWords(String sentence)
    {
    int totalbigWords, currentWordStart, currentWordEnd, pointer;
    totalbigWords = currentWordStart = currentWordEnd = pointer = 0;
    while ((pointer != sentence.length() – 1) && (!sentence.isEmpty()))
    {
    if(Character.isLetter(sentence.charAt(pointer)))
    {
    currentWordStart = pointer;
    while ((Character.isLetter(sentence.charAt(pointer))) && (pointer 5)
    {
    totalbigWords++;
    currentWordStart = currentWordEnd = pointer;
    }
    }
    else pointer++;
    } String[] bigWords = new String[totalbigWords];
    totalbigWords = currentWordStart = currentWordEnd = pointer = 0;
    while (pointer < sentence.length() – 1)
    {
    if(Character.isLetter(sentence.charAt(pointer)))
    {
    currentWordStart = pointer;
    while ((Character.isLetter(sentence.charAt(pointer))) && (pointer 5)
    {
    bigWords[totalbigWords] = sentence.substring(currentWordStart, currentWordEnd);
    totalbigWords++;
    currentWordStart = currentWordEnd = pointer;
    }
    }
    else pointer++;
    }
    return bigWords;
    }

Add comment