Ad
  • 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

    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

    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

    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 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