• Home
  • How to print even and odd position characters of an array of strings in java?

How to print even and odd position characters of an array of strings in java?

To print even and odd position characters of an array of strings in Java, you can use the following code:

String[] arr = {"string1", "string2", "string3"};

for (int i = 0; i < arr.length; i++) {
String currentString = arr[i];
for (int j = 0; j < currentString.length(); j++) {
if (j % 2 == 0) {
System.out.print(currentString.charAt(j) + " ");
}
}
System.out.println();
}

This will print out the characters at even positions for each string in the array. To print out the characters at odd positions, you can change the condition in the if statement to j % 2 != 0.