0

My code below appears to return the correct initials per the specs, but it fails the check50 test on two lines due to a NUL character at the end of my output (see below). For the two failures, when I input the names " milo banana " and " milo banana " (both with no quotes) after running ./initials, my program outputs MB in both cases, with no leading or trailing spaces. While this seems in line with the specs, check50 yields failure messages of: expected output, but not "MB\u0000\n".

Any thoughts on what I've missed would be greatly appreciated.

Here is my code:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <cs50.h>


int main(void)
{
    string name = get_string();
    if (name != NULL)
    {
        if (name[0] != ' ')
        {
            printf("%c", toupper(name[0]));
        }
        for (int i = 0, n = strlen(name); i < n; i++)
        {
            if (name[i] == ' ' && name[i+1] != ' ')
            {
                printf("%c", toupper(name[i+1]));
            }
            continue;
        }
    }
    printf("\n");    
    return 0;
}

And here is the output from check50:

~/workspace/pset2/ $ check50 2016.initials.more initials.c
:) initials.c exists
:) initials.c compiles
:) outputs "MB" for "Milo Banana"
:) outputs "MB" for "milo banana"
:( outputs "MB" for " milo  banana "
   \ expected output, but not "MB\u0000\n"
:( outputs "MB" for "  milo banana  "
   \ expected output, but not "MB\u0000\n"
:) outputs "RTB" for "   robert   thomas bowden"
:) outputs "RTB" for "Robert Thomas Bowden"
:) outputs "R" for "ROB"
:) outputs "RTB" for "Robert thomas Bowden"
https://sandbox.cs50.net/checks/316a914575c04e3ab164b66e1b5b2f13

Thank you for any help.

1

If you look carefully, each failed test has a space at the end of the string to be processed. Look at the following code:

if (name[i] == ' ' && name[i+1] != ' ')
        {
            printf("%c", toupper(name[i+1]));
        }

Think about the construction of the string as it is stored in memory. name[] is an array that has x elements, where x is the total number of characters + 1. That + 1 is to store the end of string marker, '\0'. So, if you were to store the word cat, the string array name[] would have 4 elements.

Now, look at what happens when the string has a space as the last character. First, the array index for that space will be n-1. The n-th element will be the \0 marker. So, when i = n-1, and name[i] is a space, name[i+1] (a.k.a.m name[n]) will not be equal to the ASCII value of ' '. So, it will output that value. Since it is not an alpha, toupper() will simply return whatever is given as input. In this case it returns \u0000 which is how it is displayed by check50.

In short, the code needs to handle the corner case of the last character being a space and not try to process the end of string marker. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you Cliff B - this explanation completely answered my question and helped me understand my mistake. I have clicked the check mark!
    – CodingNewb
    Jan 23 '17 at 4:35

You must log in to answer this question.

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