Algorithm


Problem Name: 30 days of code - Day 6: Let's Review

Problem Link: https://www.hackerrank.com/challenges/30-review-loop/problem?isFullScreen=true

In this HackerRank in 30 Days of Code - Day 6: Let's Review problem solution,

Objective
Today we will expand our knowledge of strings, combining it with what we have already learned about loops. Check out the Tutorial tab for learning materials and an instructional video.

Task
Given a string, S, of length N of length 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 = abdecf

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

  • 1 <= T <= 10
  • 2 <= length of S <= 10000

Output Format

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

 

 

 

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int tests;
    scanf("%d", &tests);
    
    for (int i = 0; i  <  tests; i++) {
        char s[10000];
        scanf("%s", s);
        
        int j = 0;
        while (s[j] != '\0') {
            if (j % 2 == 0) {
                printf("%c", s[j]);
            }
            j++;
        }
        printf(" ");
        j = 0;
        while (s[j] != '\0') {
            if (j % 2 != 0) {
                printf("%c", s[j]);
            }
            j++;
        }
        printf("\n");
    }
    
    return 0;
}
Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

int main() {
    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;
}
Copy The Code & Try With Live Editor

#3 Code Example with C# Programming

Code - C# Programming


using System;

class Solution
{
    static void Main(String[] args)
    {
        var N = int.Parse(Console.ReadLine());

        for (var i = 0; i  <  N; i++)
        {
            var str = Console.ReadLine();

            for (var j = 0; j  <  str.Length; j++)
            {
                if (j % 2 == 0) Console.Write(str[j]);
            }

            Console.Write(" ");

            for (var j = 0; j  <  str.Length; j++)
            {
                if (j % 2 != 0) Console.Write(str[j]);
            }

            Console.Write(Environment.NewLine);
        }
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Golang Programming

Code - Golang Programming


package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

const (
	EVEN = iota
	ODD
)

func print(oddOrEven int, words string) {
	for i := oddOrEven; i  <  len(words); i += 2 {
		fmt.Printf("%c", words[i])
	}
	fmt.Print(" ")
}

func main() {
	var T int

	fmt.Scan(&T)
	words := make([]string, T)
	reader := bufio.NewReader(os.Stdin)

	for i := 0; i  <  T; i++ {
		words[i], _ = reader.ReadString('\n')
		words[i] = strings.TrimSuffix(words[i], "\n")
	}

	for k := range words {
		print(EVEN, words[k])
		print(ODD, words[k])
		fmt.Printf("\n")
	}

}
Copy The Code & Try With Live Editor

#5 Code Example with Java Programming

Code - Java Programming


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();
    }
}
Copy The Code & Try With Live Editor

#6 Code Example with Javascript Programming

Code - Javascript Programming


function processData(input) {
    //Enter your code here
    input = input.split('\n')

    for(let i = 1; i  <  input.length; i++){
        var splitWord = input[i].split('');

        var even = '';
        var odd = '';

        for(x = 0; x  <  splitWord.length; x++){
            if(x%2 == 0){
                even = even + splitWord[x];
            }else{
                odd = odd +splitWord[x];
            }
        }
        console.log(even + ' '+ odd);
    }
}
Copy The Code & Try With Live Editor

#7 Code Example with Python Programming

Code - Python Programming


# First we have to take the input of the number of Strings 

NumberOfStrings = int(input())

# for loop from 0 to the length of the String

for i in range(0, NumberOfStrings):
    
    # Taking input from the User 
    
    string = input()
    
    # The below line has two parts 1. string[::2] & 2. string[1::2].
    # General format is [start:stop:step].
    # 1. string[::2] basically means that start from 0 to the end of the String skipping 2 characters hence taking all even strings 
    # 2. string[1::2] same as the above but we start from 1 and not 0 
    
    print(string[::2],string[1::2])
Copy The Code & Try With Live Editor

#8 Code Example with PHP Programming

Code - PHP Programming

 0 && $j % 2 != 0 ) {
			$oddString .= $s[$j];
		}
	}
	echo $evenString . ' ' . $oddString . "\n";
}

fclose( $_fp );
?>
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Day 5: Loops solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code
Next
[Solved] Day 7: Arrays solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code