Measure, Compare and Copy With the Library

Medium

This exercise uses each of the lesson's three tools once, in the order the lesson introduced them. main is written for you and needs no changes. It holds words, an array whose four elements are pointers at the string literals "delta", "alpha", "echo" and "bravo", which is chapter 4's array rule applied to the const char * you met last lesson, so words[i] is one string and count is the usual sizeof(words) / sizeof(words[0]). It also holds char long_word[] = "extraordinarily long" and char label[8], a destination deliberately far too small for it. There is no input to read, so every run produces the same seven lines. Your job is the three functions, each of which arrives with a body that does a fraction of the work. void print_length(const char *word) prints the word followed by its length, and the length comes from strlen, which walks to the null terminator and returns the count excluding it. Mind the specifier, because strlen returns size_t rather than int and the line must use %zu; %d is a type mismatch that the compiler will reject before you see the output. const char *earlier_word(const char *a, const char *b) returns whichever of the two sorts earlier alphabetically, and strcmp is the whole answer: it returns a negative number when a sorts before b, zero when they are equal, and a positive number when a sorts after, so if (strcmp(a, b) < 0) { return a; } and return b otherwise. Do not compare with == , which asks whether the two pointers hold the same address and never looks at the characters, and do not test against 1, since only the sign of a nonzero result is guaranteed. main calls this function in a loop that starts with words[0] and keeps whichever of the pair sorts earlier, so a correct comparison reduces the four words to alpha. void copy_into_label(char *label, size_t size, const char *word) does the copying, and the size parameter is there because sizeof inside this function would report the size of a pointer rather than of main's array, so the destination's size has to travel with the destination. Use the snprintf pattern from the lesson: int wanted = snprintf(label, size, "%s", word) always writes a terminator and returns the length it wanted, which is the complete length of word and not the number of characters that fit. Comparing that against size is how truncation is detected, so print label holds X, truncated from N characters when (size_t) wanted >= size and label holds X, complete otherwise. The template already performs the copy and always claims completeness, which is why its last line is wrong even though the copied text is right. Do not reach for strcpy here, and do not reach for strncpy, which would leave label with eight characters and no terminator. main returns 0 on its only path, since the checker treats a nonzero exit status as a failure however correct the printed output looks.

Success Criteria

Your code must pass 1 test case(s) to complete this exercise. 3 hint(s) are available if you need help.

Sign in to track your progress

You can work on exercises as a guest, but sign in to track your progress and save your submissions.

This platform is built by its community

Every lesson, project, and tool on HelloC++ is funded by sponsors. Join them and help shape what we build next.

Become a Patron