Print Even Length Words in Java



In this article, we will understand how to print even-length words. The string is a datatype that contains one or more characters and is enclosed in double quotes (" "). Char is a datatype that contains an alphabets an integer or a special character.

Problem Statement

Write a Java program to print even-length words. Below is a demonstration of the same ?

Input

Input string: Java Programming are cool

Output 

The words with even lengths are:
Java
cool

Approaches to print even length words

Below are the different approaches to print even length words ?

Print even length words using basic approach

Below are the steps to print even length words using basic approach ?

  • START
  • Declare a string variable input_string and assign it a value.
  • Split the input_string into individual words using the split(" ") method.
  • Use a for loop to iterate over each word. Inside the loop, use an if statement to check if the word's length is even using word.length() % 2 == 0.
  • If the condition is true.
  • Display the result
  • STOP

Java program to print even length words

Here, we bind all the operations together under the main function ?

public class EvenLengths {
  public static void main(String[] args) {
    String input_string = "Java Programming are cool";
    System.out.println("The string is defined as: " + input_string);
    System.out.println("\nThe words with even lengths are: ");
    for (String word: input_string.split(" "))
      if (word.length() % 2 == 0)
        System.out.println(word);
  }
}

Output

The string is defined as: Java Programming are cool

The words with even lengths are:
Java
cool

Print even length words using object oriented approach

Below are the steps to print even length words using basic approach ?

  • START
  • Create a static method printWords() that accepts a string parameter input_string.
  • Inside printWords(), split the input_string into individual words using the split(" ") method.
  • Use a for loop to iterate over each word.
  • Use an if statement to check if the word's length is even using word.length() % 2 == 0.
  • If the condition is true, print the word.
  • In the main method, declare input_string and call the printWords() method.
  • STOP

Java program to print even length words using object oriented programming

Here, we encapsulate the operations into functions exhibiting object-oriented programming ?

public class EvenLengths {
  public static void printWords(String input_string) {
    System.out.println("\nThe words with even lengths are: ");
    for (String word: input_string.split(" "))
      if (word.length() % 2 == 0)
        System.out.println(word);
  }
  public static void main(String[] args) {
    String input_string = "Java Programming are cool";
    System.out.println("The string is defined as: " + input_string);
    printWords(input_string);
  }
}

Output

The string is defined as: Java Programming are cool

The words with even lengths are:
Java
cool
Updated on: 2024-08-09T23:58:12+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements