Java program to print Even and Odd length words in a String.

To write a Java program to print Even and Odd length words in a String. We perform following approach.

  • We take a string word.
  • Break the string word from split method.
  • Words of the string are split and returned as a string array.
  • Traverse each word in the string array returned as string in Java.
  • From length method we find the size of word.
  • Through if condition we divide the length of word from two.
  • If length equal to zero then print Even word else print Odd word.

Syntax :- String[] words = str.split(” “);

				
					import java.util.Arrays;

public class EvenAndOddWord {
	public static void main(String[] args) {
		String h = "welcom to hello";
		for (String str : h.split(" ")) {
			if (str.length() % 2 == 0) {
				System.out.println("Even word :- " + str);
			} else {
				System.out.println("Odd word :- " + str);
			}
		}
	}
}
				
			

Output :-
Even word :- welcom
Even word :- to
Odd word :- hello

Leave a Comment

Your email address will not be published. Required fields are marked *