Input Format
The first line contains an integer,  (the number of test cases).
Each line  of the  subsequent lines contain a String, .
Output Format
For each String  (where ), print 's even-indexed characters, followed by a space, followed by 's odd-indexed characters.
Let's review problem solution - 30 days of code HackerRank

Problem solution in c++ programming language.



#include <iostream>
#include <cstring>
using namespace std;


int main() {
    int tests;
    scanf("%d", &tests);
    char input[10000];
    for(int k = 0; k < tests; k++) {
        scanf("%s", input);

        for(int i = 0; i < strlen(input); i += 2) {
            printf("%c", input[i]);
        }
        printf(" ");
        for(int i = 1; i < strlen(input); i += 2) {
            printf("%c", input[i]);
        }
        printf("\n");
    }
    return 0;
}

Problem solution in Java programming language.

#include <iostream>
#include <cstring>
using namespace std;


int main() {
    int tests;
    scanf("%d", &tests);
    char input[10000];
    for(int k = 0; k < tests; k++) {
        scanf("%s", input);

        for(int i = 0; i < strlen(input); i += 2) {
            printf("%c", input[i]);
        }
        printf(" ");
        for(int i = 1; i < strlen(input); i += 2) {
            printf("%c", input[i]);
        }
        printf("\n");
    }
    return 0;
}

Second solution



import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int testCases = scan.nextInt();
        for(int i = 0; i < testCases; i++){
            char[] inputString = scan.next().toCharArray();
            StringBuilder oddString = new StringBuilder();
            StringBuilder evenString = new StringBuilder();
   
            for(int j = 0; j < inputString.length; j++) {
                if( (j & 1) == 0) {
                    evenString.append(inputString[j]);
                }
                else {
                    oddString.append(inputString[j]);
                }
            }
            
            System.out.println(evenString + " " + oddString);
        }
        scan.close();
    }
}

Problem solution in PHP programming language.

<?php
$handle = fopen ("php://stdin","r");
$t = fgets($handle);

for($i = 0; $i < $t; $i++) { 
    $input = trim(fgets($handle));
    $even = ''; 
    $odd = '';
    
    for($j = 0; $j < strlen($input); $j++) {
        if(($j & 1) == 0) {
            $even .= $input[$j];
        }
        else {
            $odd .= $input[$j];
        }
    }

    printf("%s %s\n", $even, $odd);
}
fclose($handle);
?>

Problem solution in Python programming language.



t = int(raw_input())
for _ in range(t):
    line = raw_input()
    first = ""
    second = ""

    for i, c in enumerate(line):
        if (i & 1) == 0:
            first += c
        else:
            second += c
    print first, second

Problem solution in the Swift programming language.

import Foundation
import Darwin

let numStrings = Int(readLine()!)!

func printEvenAndOdd(string: String) {
    // This prints inputString to stderr for debugging:
    fputs("string: " + string + "\n", stderr)
    
    // Split string into an array of characters
    let chars = string.characters.map { String($0) }

    var index = 0
    while index < chars.count {
        print(chars[index], terminator: "")
        index = index + 2
    }
    print(" ", terminator: "")
 
    index = 1
    while index < chars.count {
        print(chars[index], terminator: "")
        index = index + 2
    }
    print()
}

for _ in 1...numStrings {
    let inputString = readLine()!
    printEvenAndOdd(string: inputString)
}

Problem solution in the Swift programming language.

// This is a little cleaner
import Foundation
import Darwin

let numStrings = Int(readLine()!)!

func printEveryOther(charArray: [String], startIndex: Int) {
    var index = startIndex
    while index < charArray.count {
        print(charArray[index], terminator: "")
        index = index + 2
    }
}

func printEvenAndOdd(string: String) {
    // This prints inputString to stderr for debugging:
    fputs("string: " + string + "\n", stderr)
    
    let chars = string.characters.map { String($0) }
    printEveryOther(charArray: chars, startIndex: 0)
    print(" ", terminator: "")
    printEveryOther(charArray: chars, startIndex: 1)
    print()
}

for _ in 1...numStrings {
    let inputString = readLine()!
    printEvenAndOdd(string: inputString)
}