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
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.