0

My initials program, although it appears correct, fails check50. I'm not sure why - check50 says its not printing a new line, but it does in the terminal. Please help me identify the problem:

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

int main(void)
{
    string s = get_string();   // Prompting user for name
    if (s != NULL)          // Making sure string is valid
    {
        int j = 1;

        printf("%c", toupper(s[0]));    // Printing first character in uppercase


        // Second Character (If Possible)

        do
        {
            j++;
        }
        while (s[j] != ' ' && j < strlen(s)); // Iterate through letters of string, starting with second letter, until reaches ' '

        int k;
        k = j;

        do
        {
            k++;
        }
            while (s[k] == ' ' && k < strlen(s)); //Iterate through letters of string, starting with second letter, until reaches char != ' '
        printf("%c", toupper(s[k]));


        // Third Character (If possible) 

        int l;
        l = j;

        do
        {
            l++;
        }
        while (s[l] != ' ' && l < strlen(s)); // Iterate through letters of string, starting with second letter, until reaches ' '

        int m;
        m = l;

        do
        {
            m++;
        }
        while (s[m] == ' ' && m < strlen(s));  //Iterate through letters of string, starting with second letter, until reaches char != ' '


        printf("%c", toupper(s[m]));


    }
    printf("\n"); //Print a new line
}
1

check50 gave me problems like this:

 :( outputs "MB" for "Milo Banana"
   \ expected output, but not "MB\u0000\n"

This means you are printing the M and the B and then the null character that signifies the end of the string (which will be invisible in your terminal). Make sure you don't iterate past the end of the string.

0

Make sure that the \n is within the curly braces of the function (not sure if it is because it got pushed onto the page).

1
  • The last } was accidentally not indented correctly, so showed up just after the code block. I counted the { and }, and if I haven't mis-counted, they do match, and the last printf is inside the function.
    – Blauelf
    Mar 15 '17 at 9:51

You must log in to answer this question.

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