Suppose the string is programmingwithbasics after solving this programming challenge. We will get the following output. In the below output, we can see that the pormigihais characters are coming on; even places and rgamnwtbsc are occurring in an odd place, and between them is two spaces. This is logic to solve a Day 6 Hackerrank Solution in C and other programming languages. As we know that this problem belongs to 30 days of code hackerrank challenge.
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 two 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. Get a Day 6 Let's Review Hackerrank Solution in C programming language.
Sample Input
2
Hacker
Rank
Sample Output
Hce akr
Rn ak
Explanation
Test Case 0: S="Hacker"
S[0] = "H"
S[1] = "a"
S[2] = "c"
S[3] = "k"
S[4] = "e"
S[5] = "r"
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 (Hce), and the second string contains the ordered characters from S's odd indices (akr). Let's check the second test case of Hackerrank Day 6 Solution in C
Test Case 1: S="Rank"
S[0] = "R"
S[1] = "a"
S[2] = "n"
S[3] = "k"
The even indices are 0 and 2, and the odd indices are 1 and 3. 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).
Day 6 Let's Review Hackerrank 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");
}
Explanation of Day 6 Let's Review Hackerrank Solution in C
Let's Review Hackerrank Solution in C explanation here. We first divide the string into even and odd numbers as we can see that in the string "Hacker," H is in an even place, and a is in an odd place and so on. So our string is "Hacker" and can be divided by even (H), odd (a), even (c), odd (k), even (e), and odd (r).
Step 1: The first step of Hackerrank Day 6 Solution in C. 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.
after the first loop completes the print space so we can print and separate the second loop. This is the last step of Day 6 Hackerrank Solution in C.
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.
At the end print the new line for the next input to the early and next input print in the new line.
Original Hackerrank Programming challenge:-Click Here
Step 1: The first step of Hackerrank Day 6 Solution in C. 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 completes the print space so we can print and separate the second loop. This is the last step of Day 6 Hackerrank Solution in C.
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];
}
cout<<endl;
Original Hackerrank Programming challenge:-Click Here
0 Comments: