Even Odd String
Source: Hacker rank
Task
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.
Input Format
The first line contains an integer, T (the number of test cases).
Each line of the T subsequent lines contain a String, S.
Constraints
1 <= T <= 10
2 <= length of S <= 10000
Output Format
For each String Sj (where 0 < = j <= T-1), print 's even-indexed characters, followed by a space, followed by Sj's odd-indexed characters.
Sample Input
2
Hacker
Rank
Sample Output
Hce akr
Rn ak
Explanation
Test Case 0: S="Hacker"
S[0]="H"
S[1]="a"
S[2]="c"
S[3]="k"
S[4]="e"
S[5]="r"
The even indices are 0, 2, and 4, and the odd indices are 1, 3, and 5. We then print a single line of 2 space-separated strings; the first string contains the ordered characters from S's even indices (Hce), and the second string contains the ordered characters from S's odd indices (akr).
Test Case 1: S= "Rank"
S[0]="R"
S[1]]="a"
S[2]="n"
S[3]="k"
The even indices are and 0, and 2 the odd indices are 1 and 3. We then print a single line of 2 space-separated strings; the first string contains the ordered characters from S's even indices (Rn), and the second string contains the ordered characters from S's odd indices (ak).
My solution:
C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LetsReview { class Program { static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); for (int aux = 0; aux < n; aux++) { string s = Console.ReadLine(); string even=""; string odd=""; for (int aux2=0; aux2 < s.Length; aux2++) { if(aux2 % 2 ==0) even = String.Concat(even,s[aux2]); else odd = String.Concat(odd, s[aux2]); } Console.WriteLine(even + " " + odd); } Console.ReadKey(); } } } |