0

Bizarre question! Here's the results for my check50 on my initials-more problem:

https://cs50.me/checks/815aa9b0c96b6d2a3df539987aa9d8829c5200af

It shows that my output returns "HJ" as it should do - and when I enter the same name into my problem I get the correct output, rather than what the terminal says during the checking process ("HJ\x00\n").

I'm happy to share the entirety of my code so it can be checked just in case I've screwed up somewhere, but on the one hand it says my code is correct and then not correct. I'm so confused!

I don't want to upload it and receive a lower score than I should do, if the solution is correct. I'd love to hear your thoughts :)

int main(void)
{
    // ask user for input
    string name = get_string();

    // ensure that get_string returned a string
    if (name != NULL)
    {
        // print first character, if it's not a space
        if (name[0] != ' ')
        {
            printf("%c", toupper(name[0]));
        }

        // iterate over the characters in name one at a time
        for (int i = 0, n = strlen(name); i < n; i++)
        {

            //print initials after an indetermined number of spaces
            if (name[i] == ' ' && name[i+1] != ' ')
            printf("%c", toupper(name[i+1]));
        }

        // print final space at the end of the initials
        printf("\n");

    }
}

1 Answer 1

1

It seems that we are printing the NULL character surely because we are beyond the limit of the array, to avoid it we can make a small additional check:

if (name[i] == ' ' && name[i+1] != ' ' && i + 1 < n)
2
  • Aah interesting - that did the trick! I guess what confuses me is why the error doesn't appear within my own console when I try and replicate it myself, or why in the final example, it goes beyond the limits of the array. Clearly still lots to learn! Thank you :)
    – Arianne
    Aug 20, 2017 at 20:09
  • Is a mistake that C compilers do not check, ie we can not easily know when we are overcoming the bounds of the array, sometimes there is a segmentation error, other times the program behaves strangely, and often nothing happens! , This is a problem because it is a vulnerability that hackers can take advantage of to get the control of our computer
    – MARS
    Aug 20, 2017 at 20:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .