We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Thank you so much I was struggling for nearly two days as my code was working in netbeans but I had NO idea how to get whatever input HackeRank was sending out. Here's my version, a bit more verbose than yours. Really like the logic you used:
publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);// Set the iteration for number of test casesintt=in.nextInt();for(inti=0;i<t;i++){// String variables to contain the build up of letters and final resultStringevenStr="",oddStr="",finalOutput="";// Get characters at each position and check whether 0, even or odd and // create new string based on previous values plus current oneStringinput=in.next();for(intj=0;j<input.length();j++){if(j==0||j%2==0){evenStr+=input.charAt(j);}elseif(j%2!=0){oddStr+=input.charAt(j);}// Concatenate even positions + space + odd positionsfinalOutput=evenStr+" "+oddStr;}// Print final result per test runSystem.out.println(finalOutput);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 6: Let's Review
You are viewing a single comment's thread. Return to all comments →
Thank you so much I was struggling for nearly two days as my code was working in netbeans but I had NO idea how to get whatever input HackeRank was sending out. Here's my version, a bit more verbose than yours. Really like the logic you used: