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.
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;classSolution{staticvoidMain(String[]args){intn=Convert.ToInt32(Console.ReadLine());for(inti=0;i<n;++i){stringline=Console.ReadLine();stringeven="";stringodd="";// the condition is length - 1 because// if length in odd (i.e. 5) it will iterate 3 times: j = 0, 2, 4// in this case second line (j + 1) will cause// array index out of bound exception// when it is (length - 1) it will iterate 2 times: j = 0, 2// this last letter is added after the loopfor(intj=0;j<line.Length-1;j+=2){even+=line[j];odd+=line[j+1];}// if line length is odd we need to add the last letter to resultif(line.Length%2!=0){even+=line[line.Length-1];}Console.WriteLine("{0} {1}",even,odd);}}}
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 →
Example C# solution: