Given a string of strings, write a Java program to print all even-length words in the string.
Examples:
Input: s = "This is a java language" Output: This is java language Input: s = "i am GFG" Output: am
Approach:
String[] words = str.split(" ");
for(String word : words) { }
int lengthOfWord = word.length();
Below is the implementation of the above approach:
// 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