Day 6 Let’s Review Solution | 30 Days of Code

0
94

Day 6 let’s review hackerrank solution

Day 6 Let’s Review problem Statement

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.

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 Explanation

We first divide the string into even and odd number as we can see that in the string “Hacker“, ‘H’ is in even place and ‘a’ is in an odd place and so on. So our string is “Hacker” can be divided by even(H), odd(a), even(c), odd(k), even(e), and odd(r).

Step 1:- Run the first loop up to the size of the string and find the even character and print the even place character and print like below.

for(int i=0;i<s.size();i++)
{
 if(i%2==0)
 cout<<s[i];
}

after the first loop complete print space so we can print and separate the second loop.

cout<<" ";

Step 2:- Run the second loop up to the size of the string and find the odd place character and print the odd place character and print like below.

for(int i=0;i<s.size();i++)
{
 if(i%2!=0)
 cout<<s[i];
}

At the end print the new line for the next input to the early and next input print in the new line. find below let’s review hackerrank solution.

cout<<endl;
Day 6 code explanation

Hackerrank Day 6 Solution in C

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

int main() 
{
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int n; char s[10000];
    scanf("%d",&n);
    
    for(int i=0;i<n;i++)
    {
       scanf("%s",s);
       myFunction(s);
   }
}

void myFunction(char s[])
{
    for(int i=0;i<strlen(s);i++)
    {
        if (i%2 == 0)
        {
            printf("%c",s[i]);
        }
    }
    
    printf(" ");

    for(int i=0;i<strlen(s);i++)
    {
        if (i%2 != 0)
        {
            printf("%c",s[i]);
        }
    } 
    
    printf("\n");
}

Day 6 Let’s Review Solution in 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;
}

Hackerrank Day 6 Solution in 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();
    }
}

Lets Review Output

day 6 let's review hackerrank solution

More on “30 Days of Code

LEAVE A REPLY

Please enter your comment!
Please enter your name here