C Primer Plus - Chapter 4 - Task 6 (string output) Task

Write a Program that requests the user's first name and then the user's last name. Have it print the entered names on one line and the number of letters in each name on the following line. Align each letter count with the end of the corresponding name, as in the following:

Klaus Dieter
    5      6


My Try

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

void printWhitespace(int times) {
    for (int i = 0; i &lt; times; i++) {
        printf(" ");
    }
}

int main(void) {
    // get names
    printf("first name: ");
    char firstName[20];
    scanf("%s", &amp;firstName);
    printf("last name: ");
    char lastName[20];
    scanf("%s", &amp;lastName);

    // display names
    printf("%s %s\n", firstName, lastName);

    // display number of characters under last character of names
    int numOfWhitespace = strlen(firstName) - 1;
    printWhitespace(numOfWhitespace);
    printf("%d ", strlen(firstName));

    numOfWhitespace = strlen(lastName) - 1;
    printWhitespace(numOfWhitespace);
    printf("%d\n", strlen(lastName));
}


Does the author expect that from me? Or is there a better way to write that?
Small sugggestion for printWhitespace(). You could do the following:

void printWhitespace(const unsigned int times) {
    printf("%*s", times, " ");
}


I have made the function argument const as it probably isn't meant to be modified. it is generally a good idea to be as "const" as possible as this avoids the mistake of writing to a variable that should be read-only.



I would put the array declarations at the top of the function before any of the code with a blank line between the declarations and the first line of code.



The function scanf can be used with caution... it can lead to buffer overflow attacks in the way it is used in your code. If the user enters a string longer than 19 characters (last character would be filled in as a null terminator in the buffer), scanf will just write on past the end of the buffer.

You could help guard against this by using scanf("%19s", firstName); and scanf("%19s", lastName);. As @chux points out the length is one less than the buffer size. This is because "String input conversions store a terminating null byte ('\0') to  mark end of the input; the maximum field width does not include this terminator." -- quote from man page.

Looked into this a little more and this SO answer, the author says:


  Note that the POSIX 2008 (2013) version of the scanf() family of
  functions supports a format modifier m (an assignment-allocation
  character) for string inputs (%s, %c, %[). Instead of taking a char *
  argument, it takes a char ** argument, and it allocates the necessary
  space for the value it reads


That would be a useful way of avoiding buffer overflow, but you must remember to free() the buffer returned.



The variable numOfWhitespace can also be const. Might put that to top of function too.



Your last bit of code that tries to align the numbers to the end of the words will only align properly if the string length is 9 or less. If the string length is greater then the number will be double digits so you could account for this.



Add return 0; to the end of the function. You reach the end of a non-void function without returning anything...



Hope that helps :)