Ad
  • Custom User Avatar

    Thanks a lot. I now understand what the issue was and the code works here as well.

  • Custom User Avatar

    But trashy_incel already pointed out the mistake, it's here:

    for (int j = curWord.length(); j >= 0; j--)
        revWord.push_back(curWord[j]);
    

    On the first iteration, j is out of bounds of curWord. If curWord.length() is, for example, 7, your code does curWord[7], while 7 is not a valid index.

  • Custom User Avatar

    Is there a way to debug the code here by stepping through all instructions? That's how I debugged it in Visual Studio

  • Custom User Avatar
    std::cout << int("hello"[5]);
    

    prints out 0
    or just nothing when not casting it to an int

  • Custom User Avatar

    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.

  • Custom User Avatar
    "hello".length() // returns 5
    "hello"[5] // what does this return in your compiler?
    
  • Custom User Avatar

    my problem is that it is only incorrect in the codewars compiler. The output I get from spinWords("Welcome") in my "test" in the main() function in my code is right:

     std::cout << spinWords("Welcome");
    

    Output in godbolt:

    Program returned: 0
    Program stdout
    emocleW
    

    the word "Welcome" has more than 4 letters hence it's reversed.

    Now another test:

    std::cout << spinWords("codewars compiler hates me xXxX xxxxxX");
    

    and the output:

    Program returned: 0
    Program stdout
    srawedoc relipmoc setah me xXxX Xxxxxx
    

    as you can see my algorithm is not the problem. It works everywhere but codewars.

  • Custom User Avatar

    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 code

    one mistake that i see:

    for (int j = curWord.length(); j >= 0; j--)
        revWord.push_back(curWord[j]);
    
    

    what will be the first character to be copied ?

  • Custom User Avatar

    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.

  • Custom User Avatar

    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:

    error: conflicting types for 'main'.
    ./solution.cpp:44:5: note: previous definition is here
    

    remove the main function, and your code will compile.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    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

  • Custom User Avatar

    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?