Embellished sample of the string library interface

This library contains functions that manipulate strings.  (Do not mistake this library for the obsolete library strings, that deviates a little from the standard.)

// File string.h.
// Interface of the string library.
///////////////////////////////////////////////////////////

#ifndef _STRING_H
#define _STRING_H

#include <stddef.h>


///////////////////////////////////////////////////////////
// String manipulation
///////////////////////////////////////////////////////////

// This function returns the length of the string x. In
// other words, returns the number of bytes of x not
// counting the final \0 byte. The code of the function has
// the same effect as
//       for (i = 0; x[i] != 0; ++i) ;
//       return i;
// which is equivalent to 
//       y = x;
//       while (*y++) ;
//       return y-x-1;
// Usage: k = strlen (x);

unsigned int strlen (char *x); 


// Copies the string x (including the final \0 byte) to the
// space allocated for the string y. The user must be sure
// that the space allocated to y has at least strlen(x) + 1
// bytes. The function returns y. Example:
//       char y[4];
//       strcpy (y, "ABC");
// The code of the function is equivalent to 
//       for (i = 0; (y[i] = x[i]) != 0; ++i) ;
// which, in turn, is equivalent to 
//       while (*y++ = *x++) ;
// Usage: strcpy (y, x);

char *strcpy (char *y, char *x);


// If strlen(x) < n then copies the string x (including the
// final \0 byte) onto y. If strlen(x) >= n then copies
// onto y the n first bytes of x and does not add \0 to the
// end. The user must be sure that the space allocated to y
// has at least strlen(x) + 1 or at least n bytes. The
// function returns y. Example: 
//       char y[5];
//       strncpy (y, "ABCDE", 4);
// The code of the function is equivalent to 
//       for (i = 0; i < n && x[i] != '\0'; i++)
//           y[i] = x[i];
//       for  (; i < n; i++)
//           y[i] = '\0';
// Usage: strncpy (y, x, n);

char *strncpy (char *y, char *x, size_t n);


// Concatenates the strings x and y, i.e., splices y onto
// the end of x. Returns the address of the resulting
// string, that is, returns x. The user must be sure that
// the space allocated to x is sufficient to accomodate
// strlen(y) additional bytes. Example:
//       char x[7];
//       strcpy (x, "ABC"); 
//       strcat (x, "DEF");
// The code of the function is equivalent to
//       strcpy (x + strlen (x), y);
// Usage: strcat (x, y);

char *strcat (char *, char *);



// Compares the strings x and y lexicographically, byte-
// for-byte. Returns a strictly negative number if x comes
// before y, returns 0 if x is equal to y and returns a
// strictly positive number if x comes after y. The code of
// the function is equivalent to 
//       for (i = 0; x[i] == y[i]; ++i)
//          if (x[i] == 0) return 0;
//       return x[i] - y[i];
// This, in turn, is equivalent to 
//       while (*x++ == *y++)
//          if (*(x-1) == 0) return 0;
//       return *(x-1) - *(y-1);
// Usage: if (strcmp (x, y) == 0) ... ;

int strcmp (char *x, char *y);


// Compares the character chains cx and cy represented by  
// the strings x e y. Returns a strictly negative number if
// cx comes before cy in the dictionary, returns 0 if cx
// and cy are equal, and returns a strictly positive number
// if cx comes after cy.  The behavior of the function
// depends on the value of the environment variable
// LC_COLLATE of the system. We are assuming in this site
// that the value of LC_COLLATE is en_US.UTF-8.
// Usage: if (strcoll (x, y) == 0) ... ;

int strcoll (char *x, char *y);


// The strtok ("string-to-token") function extracts tokens
// from the string s. A token is any maximal segment of s
// without delimiters. A delimiter is any byte of the
// string d. Hence, the string s has the following form:
// zero or more delimiters, followed by one or more
// nondelimiters, followed by one or more delimiters, ...,
// followed by one or more nondelimiters, and finally
// followed by zero or more delimiters.
//    The strtok function transforms each token into a
// string (by putting a \0 after the last byte of the
// token) and returns the (address of the) token. A call to
// strtok with s != NULL returns the first token of s.
// Subsequent calls with s == NULL return the second,
// third, etc. tokens. The second argument, d, can be
// different in each call to strtok. After all the tokens
// have been found, strtok returns NULL.
//    To illustrate, here is a homemade version of strtok
// restricted to the delimiters ' ' and ',':                                  
//    char *mystrtok (char *s) {
//       static char *restart;
//       char *token;
//       if (s == NULL) s = restart;
//       while (*s == ' ' || *s == ',') s++;
//       if (*s == '\0') return NULL;
//       token = s;
//       while (*s != ' ' && *s != ',' && *s != '\0') s++;
//       restart = s;
//       if (*s != '\0') {
//          *s = '\0';
//          restart++;
//       }
//       return token;
//    }
// Typical use: strtok (s, d); strtok (NULL, d);

char *strtok (char *s, const char *d);


#endif