w3resource

Java: Cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters

Java Basic: Exercise-239 with Solution

Extract Words of Length 3 to 6 from Sentence

A search engine giant such as Google accepts thousands of web pages from around the world and categorizes them, creating a huge database of information. Search engines also analyze search keywords entered by the user and create database queries based on those keywords. In both cases, complicated processing is carried out to realize efficient retrieval, but the basics are all about cutting out words from sentences.
Write a Java program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.

Input:
English sentences consisting of delimiters and alphanumeric characters are given on one line.
Output: Output a word delimited by one space character on one line.

Sample Solution:

Java Code:

// Importing the Scanner class from java.util package
import java.util.Scanner;

// Main class named "Main"
public class Main {
    public static void main(String[] args) {
        // Creating a Scanner object to read input from the console
        Scanner sc = new Scanner(System.in);

        // Prompting the user to input a sentence (max 1024 characters)
        System.out.println("Input a sentence (1024 characters max.)");

        // Reading the input sentence, removing commas and periods, and splitting into words
        String[] str = ((sc.nextLine()).replace(",", "").replace(".", "")).split(" ");

        // Initializing a flag to control space between words in the output
        int flag = 0;

        // Prompting the user about the following output
        System.out.println("\n3 to 6 characters length of words:");

        // Iterating through each word in the array
        for (String s : str) {
            // Calculating the length of the current word
            int l = s.length();

            // Checking if the length is between 3 and 6 (inclusive)
            if (l >= 3 && l <= 6) {
                // Checking if a space should be printed before the current word
                if (flag == 1) {
                    System.out.print(" ");
                }
                // Printing the current word
                System.out.print(s);

                // Updating the flag to indicate that a word has been printed
                flag = 1;
            }
        }
    }
} 

Sample Output:

Input a sentence (1024 characters. max.)
The quick brown fox

3 to 6 characters length of words:
The quick brown fox

Flowchart:

Flowchart: Restore the original string by entering the compressed string with this rule.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to restore the original string by entering the compressed string with this rule.
Next: Write a Java program that compute the maximum value of the sum of the passing integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/basic/java-basic-exercise-239.php