Open In App

Java program to print Even length words in a String

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string str, write a Java program to print all words with even length in the given string. Examples:

Input: s = "This is a java language"
Output: This
        is
        java
        language 

Input: s = "i am GFG"
Output: am

Approach:

  • Take the string
  • Break the string into words with the help of split() method in String class. It takes the string by which the sentence is to be broken. So here ” “(space) is passed as the parameter. As a result, the words of the string are split and returned as a string array
String[] words = str.split(" ");
for(String word : words)
{ }
int lengthOfWord = word.length();
  • If the length is even, then print the word.

Below is the implementation of the above approach: 

Java




// Java program to print
// even length words in a string
 
class GfG {
 public static void printWords(String s)
 {
 
  // Splits Str into all possible tokens
  for (String word : s.split(" "))
 
   // if length is even
   if (word.length() % 2 == 0)
 
    // Print the word
    System.out.println(word);
 }
 
 // Driver Code
 public static void main(String[] args)
 {
 
  String s = "i am Geeks for Geeks and a Geek";
  printWords(s);
 }
}


Output:

am
Geek

Time complexity: O(n) where n is length of given string
Auxiliary Space: O(1)

Approach 2: Using Dynamic Programming:

In this approach, we split the string into words and store the length of each word in an array. Then, we loop through the array and check if the length of each word is even. If it is, we print the word. This approach uses dynamic programming to store the length of each word in an array, which can be reused in subsequent loops, reducing the overall number of calculations. However, in practice, the difference in performance between this approach and the original approach is likely to be negligible.

Here is the DP approach in Java:

Java




class GfG {
    public static void printWords(String s) {
 
        // Split string into words
        String[] words = s.split(" ");
 
        // Create an array to store the length of each word
        int[] wordLengths = new int[words.length];
 
        // Calculate the length of each word and store in the array
        for (int i = 0; i < words.length; i++) {
            wordLengths[i] = words[i].length();
        }
 
        // Check if the length of each word is even and print if true
        for (int i = 0; i < words.length; i++) {
            if (wordLengths[i] % 2 == 0) {
                System.out.println(words[i]);
            }
        }
    }
 
    // Driver code
    public static void main(String[] args) {
 
        String s = "i am Geeks for Geeks and a Geek";
        printWords(s);
    }
}


Output:

am
Geek

Time complexity: O(n) where n is length of given string
Auxiliary Space: O(n) where n is the total number of characters in the input string.



Last Updated : 18 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads