How do I print odd and even position characters of a string set in Java?

微信扫一扫,分享到朋友圈

How do I print odd and even position characters of a string set 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!

Try to submit this:

Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
scan.nextLine();
for (int i = 0; i < T; i++) {
    String myString = scan.nextLine();
    String even = "";
    String odd = "";
    for (int j = 0; j < myString.length(); j++) {
        if (j % 2 == 0) {
            even += myString.charAt(j);
        } else {
            odd += myString.charAt(j);
        }
    }

    System.out.println(even + " " + odd);
}

i get the right output and it should meet all the requirements. i think your code fails because its not a real string you print in the end and you have empty spots in your arrays

小心!舍本逐末的销售心态重回硅谷,任何成本增长都能要了公司的命

上一篇

CCIE DC – 1.1.a Link Aggregation – Configure Virtual Port Channels

下一篇

你也可能喜欢

How do I print odd and even position characters of a string set in Java&quest;

长按储存图像,分享给朋友