0

First thank you all for taking the time to help! Working on more initials (more comfortable) and here's the problems:

1) From what I see the code gets correct results but check50 doesn't agree. Can someone tell me what went wrong?

2) I'm repeating the same lines and I know it's bad. I wanted to make a function or something and move the repeating lines out, but I just couldn't get it right. Any tips how to make this work?

Here's the code:

int main(void)
{
    printf("Name: \n");
    string n = get_string();
    //space = acsii 32
    if (n[0] != 32)
    {
        printf("%c",toupper(n[0]));
        for (int i = 0; i < strlen(n); i++)
         {
            if (n[i] == 32 && n[i+1] != 32)
            printf("%c", toupper(n[i + 1]));
         }
         printf("\n");
    }
    else
    {
        for (int i = 0; i < strlen(n); i++)
         {
            if (n[i] == 32 && n[i+1] != 32)
            printf("%c", toupper(n[i + 1]));
         }
         printf("\n");
    }
}

Here's the check50 result following by me executing the file again, I don't understand why my code doesn't pass check50?

enter image description here

2
  • Have you tried removing your printf("Name \n") function? The specification in the problem set shows you exactly what your program output should look like under Usage.
    – robert_x44
    Aug 25 '17 at 20:36
  • Removed that part thanks! But I'm still getting "expected "HJ\n", not "HJ\x00\n", seems like I'm getting a NULL somewhere somehow?
    – Chris
    Aug 25 '17 at 20:55
1

In your for loop:

for (int i = 0; i < strlen(n); i++)
{
    if (n[i] == 32 && n[i+1] != 32)
        printf("%c", toupper(n[i + 1]));
}

what happens when n[i] and n[i+1] are " " and "\0" (the last character of a string)? If the name ends in a space, these will be the last two characters - a space and NULL. You're calling toupper() on the NULL character and printing it.

1
  • Thank you! I added n[i+1] != 0 to make it work but I didn't know why. Your answer helps me understand it.
    – Chris
    Aug 25 '17 at 21:10

You must log in to answer this question.

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