C
C#2y ago
shawski.

❔ need help with a problem

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int t = int.Parse(Console.ReadLine());

for (int i = 0; i < t; i++){
string s = Console.ReadLine();
for (int o = 0; o < s.Length; o++){
if (o % 2 == 0){
Console.Write(s[i]);
}
}
for (int o = 0; o < s.Length; o++){
if (o % 2 == 1){
Console.Write(s[i]);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int t = int.Parse(Console.ReadLine());

for (int i = 0; i < t; i++){
string s = Console.ReadLine();
for (int o = 0; o < s.Length; o++){
if (o % 2 == 0){
Console.Write(s[i]);
}
}
for (int o = 0; o < s.Length; o++){
if (o % 2 == 1){
Console.Write(s[i]);
}
}
}
}
}
I wrote this code. The input in this case was "Hacker Rank". It's supposed to output "Hce akr Rn ak". Instead, it outputted "HHHHHHaaaa". I have no idea why this happened. I must've messed up somewhere.
17 Replies
Deneri
Deneri2y ago
Double check which indexer (o,i) you're using in which places. I think that will get you closer. It's hard to tell what you are trying to do, but I think that's a big first problem.
shawski.
shawski.2y ago
Alright heres what im trying to do t is the amount of words that will be printed s is the word(s) that are inputted i am trying to print the letter every even index then next to that it should print the letters with an odd index "Hacker Rank" should turn into
Hce
Akr
Hce
Akr
Hce being the letters that have an even index Akr being the letters with an odd index i hope that makes sense i also realized i wrote s[i] instead of s[o] ill fix that and see what that does alright i can fix it that was the mistake, using i instead of o thank you for pointing that out
Kouhai
Kouhai2y ago
Instead of looping twice You can construct two strings and then print them out
shawski.
shawski.2y ago
?
Kouhai
Kouhai2y ago
You're looping twice, you can reduce it to one loop
shawski.
shawski.2y ago
not sure how that would work
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int t = int.Parse(Console.ReadLine());

for (int i = 0; i < t; i++){
string s = Console.ReadLine();
for (int o = 0; o < s.Length; o++){
if (o % 2 == 0){
Console.Write(s[o]);
}
}
Console.Write(" ");
for (int o = 0; o < s.Length; o++){
if (o % 2 == 1){
Console.Write(s[o]);
}
}
Console.WriteLine("");
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int t = int.Parse(Console.ReadLine());

for (int i = 0; i < t; i++){
string s = Console.ReadLine();
for (int o = 0; o < s.Length; o++){
if (o % 2 == 0){
Console.Write(s[o]);
}
}
Console.Write(" ");
for (int o = 0; o < s.Length; o++){
if (o % 2 == 1){
Console.Write(s[o]);
}
}
Console.WriteLine("");
}
}
}
alright i finished fixing everything up and formatting it. it passed all the tests would be open to how i can improve this
Kouhai
Kouhai2y ago
To avoid looping twice
string s = Console.ReadLine();
Span<char> even = stackalloc char[(s.Length + 1) / 2];
Span<char> odd = stackalloc char[s.Length / 2];
for (int evenIndex = 0, oddIndex = 0, strIndex = 0; strIndex < s.Length; strIndex++)
{
if (strIndex % 2 == 0)
{
even[evenIndex++] = s[strIndex];
}
else
{
odd[oddIndex++] = s[strIndex];
}
}
Console.Write(even.ToString());
Console.Write(" ");
Console.Write(odd.ToString());
string s = Console.ReadLine();
Span<char> even = stackalloc char[(s.Length + 1) / 2];
Span<char> odd = stackalloc char[s.Length / 2];
for (int evenIndex = 0, oddIndex = 0, strIndex = 0; strIndex < s.Length; strIndex++)
{
if (strIndex % 2 == 0)
{
even[evenIndex++] = s[strIndex];
}
else
{
odd[oddIndex++] = s[strIndex];
}
}
Console.Write(even.ToString());
Console.Write(" ");
Console.Write(odd.ToString());
Or to use one buffer
Span<char> str = stackalloc char[s.Length + 1];
int offset = (s.Length + 1) / 2;
str[offset] = ' ';
for (int evenIndex = 0, oddIndex = offset+1, strIndex = 0; strIndex < s.Length; strIndex++)
{
if (strIndex % 2 == 0)
{
str[evenIndex++] = s[strIndex];
}
else
{
str[oddIndex++] = s[strIndex];
}
}
Console.Write(str.ToString());
Span<char> str = stackalloc char[s.Length + 1];
int offset = (s.Length + 1) / 2;
str[offset] = ' ';
for (int evenIndex = 0, oddIndex = offset+1, strIndex = 0; strIndex < s.Length; strIndex++)
{
if (strIndex % 2 == 0)
{
str[evenIndex++] = s[strIndex];
}
else
{
str[oddIndex++] = s[strIndex];
}
}
Console.Write(str.ToString());
You can use char[] instead of Span<char> if you don't want to use spans
shawski.
shawski.2y ago
i dont really understand some of the stuff you did
Kouhai
Kouhai2y ago
In the first or second example?
shawski.
shawski.2y ago
both i havent seen
Span<char>
Span<char>
Kouhai
Kouhai2y ago
a Span<char> is a buffer that will hold char It's similar to an array, the difference is that it lives on the stack, i.e better performance.
shawski.
shawski.2y ago
so should that be used as a replacement for an array?
Kouhai
Kouhai2y ago
In this case, using it can replace an array (in other cases it can't) You can do this exact code with arrays, if you don't want to get into Spans and stuff
string s = Console.ReadLine();
char[] even = new char[(s.Length + 1) / 2];
char[] odd = new char[s.Length / 2];
for (int evenIndex = 0, oddIndex = 0, strIndex = 0; strIndex < s.Length; strIndex++)
{
if (strIndex % 2 == 0)
{
even[evenIndex++] = s[strIndex];
}
else
{
odd[oddIndex++] = s[strIndex];
}
}
Console.Write(even.ToString());
Console.Write(" ");
Console.Write(odd.ToString());
string s = Console.ReadLine();
char[] even = new char[(s.Length + 1) / 2];
char[] odd = new char[s.Length / 2];
for (int evenIndex = 0, oddIndex = 0, strIndex = 0; strIndex < s.Length; strIndex++)
{
if (strIndex % 2 == 0)
{
even[evenIndex++] = s[strIndex];
}
else
{
odd[oddIndex++] = s[strIndex];
}
}
Console.Write(even.ToString());
Console.Write(" ");
Console.Write(odd.ToString());
char[] str = new char[s.Length + 1];
int offset = (s.Length + 1) / 2;
str[offset] = ' ';
for (int evenIndex = 0, oddIndex = offset+1, strIndex = 0; strIndex < s.Length; strIndex++)
{
if (strIndex % 2 == 0)
{
str[evenIndex++] = s[strIndex];
}
else
{
str[oddIndex++] = s[strIndex];
}
}
Console.Write(str.ToString());
char[] str = new char[s.Length + 1];
int offset = (s.Length + 1) / 2;
str[offset] = ' ';
for (int evenIndex = 0, oddIndex = offset+1, strIndex = 0; strIndex < s.Length; strIndex++)
{
if (strIndex % 2 == 0)
{
str[evenIndex++] = s[strIndex];
}
else
{
str[oddIndex++] = s[strIndex];
}
}
Console.Write(str.ToString());
Exact code but with arrays instead of spans
shawski.
shawski.2y ago
and this would work with a list as well? also whats this line doing?
str[offset] = ' ';
str[offset] = ' ';
Kouhai
Kouhai2y ago
Just a small fix, with the array example you'll need Console.Write(new string(str)); ToString() won't work You'll need to convert the list to an array This adds a space between even and odd index letters similar to this line in your code Console.Write(" ");
shawski.
shawski.2y ago
oh i think im a little bit off from writing the type of code you gave me ill get there thank you. im gonna leave this ticket open a little longer so i can look at some of the stuff you sent.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.