Java Day 6: Let's Review



Given a string, S , of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).

Note: 0 is considered to be an even index.



  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.Scanner;
  4. public class Solution {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. int N = in.nextInt();
  8. in.nextLine();
  9. for (int i = 0; i < N; i++) {
  10. String string = in.nextLine();
  11. char[] charArray = string.toCharArray();
  12. for (int j = 0; j < charArray.length; j++) {
  13. if (j % 2 == 0) {
  14. System.out.print(charArray[j]);
  15. }
  16. }
  17. System.out.print(" ");
  18. for (int j = 0; j < charArray.length; j++) {
  19. if (j % 2 != 0) {
  20. System.out.print(charArray[j]);
  21. }
  22. }
  23. System.out.println();
  24. }
  25. in.close();
  26. }
  27. }