I am studying C from Stephen Prata’s C Primer Plus. Here is one question from chapter 11.
Write a program that reads in up to 10 strings or to EOF, whichever comes
first. Have it offer the user a menu with five choices: print the original
list of strings, print the strings in ASCII collating sequence,
print the strings in order
of increasing length, print the strings in order of the length of the first word
in the string, and quit. Have the menu recycle until the user enters the quit
request. The program, of course, should actually perform the promised tasks
I understand the problem except for the bit where it asks “print the strings in ASCII collating sequence”. What does it mean? Does it mean that I have to change the
order of characters as in ASCII collating sequence?
Collation means the “logical” ordering of strings. For the “C” locale, this is indeed equivalent to sorting by ASCII codes (using
strcmp()).However, collation is locale-dependent.
For example, in ISO 8859-1 encoding and DE locale, the letter “ü” (0xfc) shouldn’t end up after “z” (0x7a) / at the end of the list, as it would with a
strcmp()sorting, but be sorted equivalent to “ue” (0x75 0x65)…And because you can’t probably know all facts about all existing locales, the standard library provides
strcoll(),strxfrm(),wcscoll(),wcsxfrm()and theLC_COLLATEoption tosetlocale(), which do the locale-specific stuff for you.Welcome to the world of internationalization. 😉 Lucky the question explicitly asks for “ASCII collating”, which means you get away with
strcmp()…