To write a program to print all the Even length words in the given string.
Using split() method :-
- The split() method is a built in String class.this method break the string into words.
- Here ” “(space) is pass as a parameter when sentencetring is broken.after that String is return as a string array.
Syntax String[] kt = str.split(” “);
- Traverse the each word of string array and return to string.
- Inside the if condition calculate the length of each word then do modulo by 2 of string length.
- If It return 0 means the length is even, then print the word.
public class EvenString {
public static void main(String[] args) {
String str = "i am a developer";
String[] kt = str.split(" ");
System.out.println("Even Words String are :- ");
for (String j : kt) {
if (j.length() % 2 == 0) {
System.out.println(j);
}
}
}
}
Output :-
Even Words String are :-
am