Introduction:
Welcome to Day 6 of the 30 Days of Code challenge! Today, we'll explore the combination of strings and loops to manipulate and print characters at even and odd indices. Understanding string manipulation is a crucial skill in programming, and this challenge will help reinforce your knowledge.
The Challenge:
Given a string, s, of length n, we are tasked with printing its even-indexed and odd-indexed characters as space-separated strings on a single line. In this context, indices are considered even when they are divisible by 2.
Format & Sample:
For example, if we have the string "Hacker," the output should be "Hce akr." The even-indexed characters are 'H,' 'c,' and 'r,' while the odd-indexed characters are 'a,' 'k,' and 'e.'
Input Format:
The first line contains an integer, t (the number of test cases).
Each of the t subsequent lines contains a string, s.
Output Format:
For each string s (where 1 <= i <= t), 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 0, 2, and 4, while the odd indices are 1, 3, and 5. The output is a single line of space-separated strings, where the first string contains characters from even indices, and the second string contains characters from odd indices.
Test Case 1: The even indices are 0 and 2, while the odd indices are 1 and 3. The output follows the same format as Test Case 0.
Code Breakdown:
Reading Input:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
cin.ignore(); // Consume newline
while (t--) {
string s;
getline(cin, s);
// Code for processing each test case goes here
}
return 0;
}
This code reads the number of test cases, t, and then reads each string s for the respective test cases.
Printing Output:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
cin.ignore(); // Consume newline
while (t--) {
string s;
getline(cin, s);
// Print even-indexed characters
for (int i = 0; i < s.length(); i += 2) {
cout << s[i];
}
cout << " ";
// Print odd-indexed characters
for (int i = 1; i < s.length(); i += 2) {
cout << s[i];
}
cout << endl;
}
return 0;
}
Within the loop, this code prints even-indexed characters followed by a space and then prints odd-indexed characters.
Final Code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
cin.ignore(); // Consume newline
while (t--) {
string s;
getline(cin, s);
// Print even-indexed characters
for (int i = 0; i < s.length(); i += 2) {
cout << s[i];
}
cout << " ";
// Print odd-indexed characters
for (int i = 1; i < s.length(); i += 2) {
cout << s[i];
}
cout << endl;
}
return 0;
}
We hope you enjoy working on Day 6 of the 30 Days of Code challenge! Feel free to explore more challenges and enhance your programming skills.