The C library includes a fair number of operations on C-strings, which we access by including the string.h header file. These functions are part of the Standard library, so we don't need to link any special libraries to use them.
We had a question about finding the binary point in a C-string, such as:
Lets assume this C-string is referenced by a pointer variable named
digits (it could also be stored in an array named digits, and things will work the same). Then one way to find the position of the binary point would be:
char *pos = strchr(digits, '.'); // find first matching character in string
If the character '.' is found in
pos will contain the address (not the index!) of that character. This function finds the first such character in the C-string. It returns NULL if the character is not found.
A trick for dividing the entire string of
digits into two strings containing the left half (the whole part) and the right half (the fractional part) is to replace the '.' with ASCII 0, the nil-terminator.
You can find matching characters in the other direction this way:
char *pos = strrchr(digits, '.'); // find last matching character in string
The extra r in the name means this is a “reverse search” so we'll find the last matching '.' character.
King Chapter 13 covers the basics of processing C-strings and the most important functions on them (
strcmp). Section 23.5 covers the entire set of string-handling functions included in string.h.