Ad
  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    Yes, that did fix it, thank you very much. And thank you for the reminder of bad practice.

  • Custom User Avatar

    there may be other mistakes, but here is the first that i saw:

    char ccopy[(int) clen], icopy[(int) ilen];
    strcpy(ccopy, code);
    strcpy(icopy, input);
    

    you did not reserve 1 byte for the nul character ('\0') in ccopy and icopy, thus the strcpy() calls are undefined behavior and anything can happen during program execution. (also, variable-length arrays are bad style and should not be used in clean code)

    fix this, and see if your solution works

  • Custom User Avatar

    Hi, I'm doing this kata in C. My code runs fine until the "more complex examples with randomness", where the output is just "Expected: Hello World!, but got: (BLANK)". I printed the inputs so that I can run it on my IDE (Codeblocks) and it runs fine, outputting "Hello World!" as expected. The codes are the exact same, so I'm not sure what is wrong. Anybody has any ideas?

  • Custom User Avatar

    Found the issue... it was a mis-reinitialized variable. THanks all!

  • Custom User Avatar

    Wait a minute, I misunderstood your comment. I tried an input of "abcde abcde" and it returned "edcba abcde", didn;t invert the second one.

  • Custom User Avatar

    I tried that and it works, but as soon as there are multiple words with a 5 letter words it skips the 5-letter. For example, "abcde" ==> "edcba", BUT "Just kidding there is still one more" ==> "Just gniddik there is still one more", where it didn't invert the words "there" and "still".

  • Custom User Avatar

    call your function with this input, and see what happens : "abcde abcde"

    also, style notes: every single cast in your code has no effect. you should use casts more sparingly, and in particular never cast the return of malloc(): it is not only useless but harmful, as it suppresses type checking by the compiler. it is a remnant of the ancient times in which C did not have a void * type, and malloc() returned a char *

  • Custom User Avatar

    But then I'm not even sure about that.. doesn't quite make sense if only 5 letter words are not inverted

  • Custom User Avatar

    Yup, unfortunately I'm using all the right statements... so idk if it's a problem with arrays overstepping bounds and erasing the length values.

  • 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

  • Loading more items...