❔ need help with a problem
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
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.
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 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
Instead of looping twice
You can construct two strings and then print them out
?
You're looping twice, you can reduce it to one loop
not sure how that would work
alright i finished fixing everything up and formatting it. it passed all the tests
would be open to how i can improve this
To avoid looping twice
Or to use one buffer
You can use
char[]
instead of Span<char>
if you don't want to use spansi dont really understand some of the stuff you did
In the first or second example?
both
i havent seen
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.so should that be used as a replacement for an array?
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
Exact code but with arrays instead of spans
and this would work with a list as well?
also
whats this line doing?
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(" ");
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.
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.