In Java, we can find and print all words having an even number of characters in a given string. This can be done by splitting the string into words and checking the length of each word.
- split() divides the string into individual words.
- length() % 2 == 0 checks whether the length of a word is even.
Examples
Input: s = "i am Geeks for Geeks and a Geek"
Output: am
GeekInput: s = "Java is a powerful language"
Output: Java
is
powerful
language
Approach
The simplest way to solve this problem is:
- Take the input string.
- Split the string into individual words using the split() method.
- Traverse each word.
- Find the length of each word using length().
- If the length is even, print the word.
public class EvenLengthWords {
public static void printWords(String s) {
// Split the string into words
String[] words = s.split(" ");
// Traverse each word
for (String word : words) {
// Check if the length is even
if (word.length() % 2 == 0) {
System.out.println(word);
}
}
}
public static void main(String[] args) {
String s = "Java is a powerful language";
printWords(s);
}
}
Output
Java is powerful language
Explanation: The split(" ") method divides the given string into individual words. The program then checks the length of each word using length(). If word.length() % 2 == 0, the word has an even number of characters and is printed.
For the given input:
Java -> 4 - > even -> print
is -> 2 -> even -> print
a -> 1 -> odd -> skip
powerful -> 8 -> even -> print
language -> 8 -> even -> print
Note: If using the input above, the complete output will include powerful and language as well because both contain 8 characters.
Methods to Print Even Length Words in a String
There are several ways to solve this problem in Java:
1. Using String.split()
The split() method is the simplest and most commonly used approach. It divides a string into an array of words based on the specified delimiter.
Approach
- Split the string using space " ".
- Traverse the resulting array.
- Check the length of every word.
- Print the word if its length is even.
public class EvenLengthWords {
public static void printWords(String s) {
String[] words = s.split(" ");
for (String word : words) {
if (word.length() % 2 == 0) {
System.out.println(word);
}
}
}
public static void main(String[] args) {
String s = "Java is a powerful language";
printWords(s);
}
}
Output
Java is powerful language
Explanation: The program splits the given string into individual words using split(" ").It checks the length of each word, and if the length is even (length() % 2 == 0), it prints that word.
2. Using a Traditional for Loop
Instead of using a for-each loop, we can use a traditional for loop to traverse the array of words.
public class EvenLengthWords {
public static void main(String[] args) {
String s = "Coding makes problem solving easier";
String[] words = s.split(" ");
for (int i = 0; i < words.length; i++) {
if (words[i].length() % 2 == 0) {
System.out.println(words[i]);
}
}
}
}
Output
Coding easier
Explanation: Above program splits the given string into individual words using split(" ") and checks the length of each word. If a word has an even number of characters, it prints that word using the condition words[i].length() % 2 == 0.
3. Using StringTokenizer
Java's StringTokenizer class can also be used to divide a sentence into individual words.
import java.util.StringTokenizer;
public class EvenLengthWords {
public static void main(String[] args) {
String s = "Learn Java with simple examples";
StringTokenizer st =
new StringTokenizer(s);
while (st.hasMoreTokens()) {
String word = st.nextToken();
if (word.length() % 2 == 0) {
System.out.println(word);
}
}
}
}
Output
Java with simple examples
Explanation: Above program uses StringTokenizer to extract each word from the given string one by one. It checks the length of each word and prints it if its length is even using word.length() % 2 == 0.
4. Without Using split()
We can also solve the problem without using the split() method. In this approach, we traverse the string character by character and keep track of the length of the current word. When a space is encountered, we know that the current word has ended. We then check whether its length is even.
public class EvenLengthWords {
public static void printWords(String s) {
String word = "";
for (int i = 0; i <= s.length(); i++) {
if (i < s.length() && s.charAt(i) != ' ') {
word += s.charAt(i);
} else {
if (word.length() > 0 &&
word.length() % 2 == 0) {
System.out.println(word);
}
word = "";
}
}
}
public static void main(String[] args) {
String s = "Java code makes learning easier";
printWords(s);
}
}
Output
Java code learning easier
Explanation: The program manually extracts each word from the string without using split() and checks its length.
If a word has an even number of characters, it prints the word; then it resets and continues with the next word.