Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Thanks a lot. I now understand what the issue was and the code works here as well.
But
trashy_incel
already pointed out the mistake, it's here:On the first iteration,
j
is out of bounds ofcurWord
. IfcurWord.length()
is, for example, 7, your code doescurWord[7]
, while 7 is not a valid index.Is there a way to debug the code here by stepping through all instructions? That's how I debugged it in Visual Studio
prints out 0
or just nothing when not casting it to an int
It does not really matter that the code works in some place or another, if it's incorrect. Your solution has an out-of-bounds read. It attempts to read from out of bounds of a string. This is UB, and program working anywhere else does not really prove anything. Joys of C++, where invalid code is not required to fail.
Your algorithm is not a problem, your algorithm is OK. Your implementation is the problem, it has a bug, and causes your solution to fail.
my problem is that it is only incorrect in the codewars compiler. The output I get from
spinWords("Welcome")
in my "test" in themain()
function in my code is right:Output in godbolt:
the word "Welcome" has more than 4 letters hence it's reversed.
Now another test:
and the output:
as you can see my algorithm is not the problem. It works everywhere but codewars.
i had mistakenly understood that your code failed to compile, sorry. Your code is incorrect, it fails for example the very first sample test
"Welcome"
. you should read the logs that show you the input, expected and actual values and debug your codeone mistake that i see:
what will be the first character to be copied ?
Hi again, sorry for the late reply to my own question, but the main function was only for the code to compile on godbolt and I removed it whenever testing it on codewars. In main() I just cout'ed the output from my spinWords to show that the algorithm works. The test has the same input as in cout in the main() function. In main(), my function outputs the same as the expected output from the test but fails the test when compiled on codewars.
On Codewars, you write code (usually one or several functions) that will be tried against a test suite. Your code and the tests suite are run in the same process (you can see your code as constituting a small library). Hence, you are not supposed to define your own
main()
function: the tests suite already contains one. Thus, the compiler complains:remove the
main
function, and your code will compile.This comment is hidden because it contains spoiler information about the solution
this is not an issue, this is a question ;-)
you do not provide any details. what code ? what error ?
also note that Codewars only supports C++17, so C++23 code is not going to compile
see this. if it doesnt help with your problem and you're still stuck, post your code with a spoiler flag and markdown formatting in a reply to this comment and we can have a look
I coded (C++) the algorithm in visual studio, everything works. I copied the code in here and suddenly it's broken. Compiles and works on godbolt and same compiler. Any ideas?