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

Question

Given a string S of length N, that is indexed from 0 to N-1, print it’s even indexed and odd indexed characters as 2 space separated strings on a single line. Assume input starts at index position 0(which is considered even)

Input

The first line contains an integer, T (the number of test cases). Each line i of the T subsequent lines contain a String, S.

Output

For each string S, print it’s even-indexed characters, followed by space, followed by odd-indexed characters.

Sample Input

2

Hacker

Rank

Sample Output

Hce akr

Rn ak

The Code I Wrote

public static void main(String[] args)
{
    Scanner scan    =   new Scanner(System.in);
    int T   =   scan.nextInt();
    scan.nextLine();

    for(int i=0 ; i<T ; i++)
    {
        String  myString    =   scan.nextLine();

        int evn =   0,
            odd =   0,
            len =   myString.length();

        char    strE[]  =   new char[50],
                strO[]  =   new char[50];

        for(int j=0 ; j<len ; j++)
        {
            if(j%2 == 0)
            {
                strE[evn]   =   myString.charAt(j);
                evn++;
            }
            if(j%2 == 1)
            {
                strO[odd]   =   myString.charAt(j);
                odd++;
            }
        }
        System.out.print(strE);
        System.out.print(" ");
        System.out.println(strO);
    }
}

My Output

Hce akr

Rn ak

The Problem

As you can see, my program successfully meets the test case, and other test cases (using custom input) but every time the HackerRank compiler tells me that my program did not meet the test case.

Clearly, my program is producing the required output but every time the HackerRank compiler tells me that I got the solution wrong.

Could anyone please tell me where I am making a mistake?

Further Modifications

I then decided to change the last 3 lines of print statements into one statement as follows:

System.out.println(strE + " " + strO);

However, this time the program did not produce the desired output and rather printed some garbage values as follows:

[[email protected] [[email protected]

[[email protected] [[email protected]

My Doubts

1. In the first case, when I was printing the two strings separately using 2 print statements, I was getting a correct output everytime but the HackerRank compiler rejected it. Why?

2. In the second case, when I modified the program by using one print statement instead of 3 to get the desired result, the program gave a completely different output and rather printed garbage values! Why?

Here is a link to the HackerRank problem for more info: hackerrank.com/challenges/30-review-loop

All help and guidance is greatly appreciated and thanks a lot in advance!

Answer

int T = scan.nextInt();

This reads quantity of test cases, which we’re going to process.

String string[] = new String[T];
for(int i = 0; i<T; i++){
  string[i] = scan.next();

} Next we’re creating an array named “string” (BTW, this a bad name for variables/objects) which has size T and in the for loop reading test cases from the input T times and saving them in the array.

for(int temp = 0; temp<T; temp++){

Now, for each of test cases we do the following…

for(int j = 0; j<string[temp].length(); j = j+2)
{
    System.out.print(string[temp].charAt(j));
}

We create a local variable j, which is visible only in this for loop. j holds index of the string (=string[temp]), which we’re processing. So, we’re printing a character on position j (by using standard method “charAt” of String class, which returns character of given index of the string) and then increasing it by 2. So, this code will print every even character. For string “example”, it will print “eape” (j=0, j=2, j=4, j=6).

System.out.print(" ");

Separating sequences with a space.

for(int j = 1; j<string[temp].length(); j = j+2){
    System.out.print(string[temp].charAt(j));
}

System.out.println();

We’re doing the same (creating index j, running though all characters of the string), but starting from “1”, so it will print all odd characters of the string. For string “example”, it will give you “xml” (j=1, j=3, j=5). and After this, it will end the string. I hope, it will help you to understand. 🙂

Leave a Reply

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