Hello coders, today we are going to solve Day 6: Let’s Review HackerRank Solution in C++, Java and Python.
Objective
Today we will expand our knowledge of strings, combining it with what we have already learned about loops.
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.
Example
s = adbecf
Print abc def
Input Format
The first line contains an integer, T (the number of test cases).
Each line i of the T subsequent lines contain a string, S.
Constraints
For each String Sj (where 0 <= j <= T – 1), print Sj‘s even-indexed characters, followed by a space, followed by Sj‘s odd-indexed characters.
Sample Input
1 2 3 |
2 Hacker Rank |
Sample Output
1 2 |
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 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 (Rn), and the second string contains the ordered characters from S‘s odd indices (ak).
Solution – Day 6: Let’s Review
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int N; cin >> N; for (int i = 0; i < N; i++) { string str; cin >> str; for (int j = 0; j < str.length(); j++) { if (j % 2 == 0) { cout << str[j]; } } cout << " "; for (int j = 0; j < str.length(); j++) { if (j % 2 != 0) { cout << str[j]; } } cout << endl; } return 0; } |
Java
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); in.nextLine(); for (int i = 0; i < N; i++) { String string = in.nextLine(); char[] charArray = string.toCharArray(); for (int j = 0; j < charArray.length; j++) { if (j % 2 == 0) { System.out.print(charArray[j]); } } System.out.print(" "); for (int j = 0; j < charArray.length; j++) { if (j % 2 != 0) { System.out.print(charArray[j]); } } System.out.println(); } in.close(); } } |
Python
# Enter your code here. Read input from STDIN. Print output to STDOUT
1 2 3 4 5 6 7 |
T = int(input()) for i in range (0 , T): S = input() print(S[0::2] + " " + S[1::2]) |
Disclaimer: The above Problem (Day 6: Let’s Review) is generated by Hacker Rank but the Solution in Provided by Learnkro. This tutorial is only for Educational and Learning Purpose.