Breaking

Thursday, 21 September 2017

Day 6: Let's Review

              Day 6: Let's Review


Objective
Today we're expanding our knowledge of Strings and combining it with what we've already learned about loops. Check out the Tutorial tab for learning materials and an instructional video!
Task
Given a string, , of length  that is indexed from  to , print its even-indexed and odd-indexed characters as  space-separated strings on a single line (see the Sample below for more detail).
Note:  is considered to be an even index.
Input Format
The first line contains an integer,  (the number of test cases).
Each line  of the  subsequent lines contain a String, .
Constraints
Output Format
For each String  (where ), print 's even-indexed characters, followed by a space, followed by 's odd-indexed characters.
Sample Input
2
Hacker
Rank
Sample Output
Hce akr
Rn ak
Explanation
Test Case 0






The even indices are , and , and the odd indices are , and . We then print a single line of  space-separated strings; the first string contains the ordered characters from 's even indices (), and the second string contains the ordered characters from 's odd indices ().
Test Case 1




The even indices are  and , and the odd indices are  and . We then print a single line of  space-separated strings; the first string contains the ordered characters from 's even indices (), and the second string contains the ordered characters from 's odd indices ().
Code:

C++:



#include <iostream>

using namespace std;

int main() {
    int N;
    cin >> N;
    string tmp;
    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.util.Scanner;

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:

N = int(input())

for i in range(0, N):

    string = input()

    for j in range(0, len(string)):
        if j % 2 == 0:
            print(string[j], end='')

    print(" ", end='')

    for j in range(0, len(string)):
        if j % 2 != 0:
            print(string[j], end='')


    print("")


Please do comment If u have any Queries!\

No comments:

Post a Comment

Like