Day 6: Let’s Review | 30 Days Of Code | HackerRank Solution

Hello coders, today we are going to solve Day 6: Let’s Review HackerRank Solution in C++, Java and Python.

Day 6: Let's Review

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

2
Hacker
Rank

Sample Output

Hce akr
Rn ak

Explanation

Test Case 0S = “Hacker”
S[0] = “H”
S[1] = “a”
S[2] = “c”
S[3] = “k”
S[4] = “e”
S[5] = “r”
The even indices are 02, and 4, and the odd indices are 13, 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 1S = “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++

#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

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
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 CodingBroz. This tutorial is only for Educational and Learning Purpose.

3 thoughts on “Day 6: Let’s Review | 30 Days Of Code | HackerRank Solution”

  1. Md Nasir Uddin

    This is another way to solved this program Using java:

    import java.io.*;
    import java.util.*;

    public class Solution {

    public static void main(String[] args) {

    Scanner scan=new Scanner(System.in);
    int N = scan.nextInt();
    scan.nextLine();
    for(int i=0;i<N;i++){
    String S=scan.next();
    String evenResult="";
    String oddResult="";
    for(int a=0;a<S.length();a++){
    if(a%2==0)
    evenResult+=S.charAt(a);
    else
    oddResult+=S.charAt(a);
    }

    System.out.println(evenResult+" "+oddResult);
    }

    scan.close();
    }
    }

Leave a Comment

Your email address will not be published. Required fields are marked *