3.4 Program Examples

Example 3-4 shows how to use the strcat and strncat functions.

Example 3-4 Concatenating Two Strings

/*  This example uses strcat and strncat to concatenate two    *
 *  strings.                                                   */

#include <stdio.h>
#include <string.h>

main()
{
    static char string1[80] = "Concatenates ";
    static char string2[] = "two strings ";
    static char string3[] = "up to a maximum of characters.";
    static char string4[] = "imum number of characters";

    printf("strcat:\t%s\n", strcat(string1, string2));
    printf("strncat ( 0):\t%s\n", strncat(string1, string3,  0));
    printf("strncat (11):\t%s\n", strncat(string1, string3, 11));
    printf("strncat (40):\t%s\n", strncat(string1, string4, 40));
}

Example 3-4 produces the following output:

$ RUN  EXAMPLE1
strcat: Concatenates two strings
strncat ( 0): Concatenates two strings
strncat (11): Concatenates two strings up to a max
strncat (40): Concatenates two strings up to a maximum number of characters.
$

Example 3-5 shows how to use the strcspn function.

Example 3-5 Four Arguments to the strcspn Function

/*  This example shows how strcspn interprets four             *
 *  different kinds of arguments.                              */

#include <stdio.h>

main()
{
   FILE *outfile;
   outfile = fopen("strcspn.out", "w");

   fprintf(outfile, "strcspn with null charset: %d\n",
                     strcspn("abcdef", ""));

   fprintf(outfile, "strcspn with null string: %d\n",
                     strcspn("", "abcdef"));

   fprintf(outfile, "strcspn(\"xabc\", \"abc\"): %d\n",
                     strcspn("xabc", "abc"));

   fprintf(outfile, "strcspn(\"abc\", \"def\"): %d\n",
                     strcspn("abc", "def"));
}

The sample output, to the file strcspn.out, in Example 3-5 is as follows:

$ RUN  EXAMPLE2 

strcspn with null charset:  6
strcspn with null string:  0
strcspn("xabc","abc"):  1
strcspn("abc","def"):  3

Example 3-6 shows how to use the <stdarg.h> functions and definitions.

Example 3-6 Using the <stdarg.h> Functions and Definitions

/* This routine accepts a variable number of string arguments,   *
 * preceded by a count of the number of such strings. It         *
 * allocates enough space in which to concatenate all of the     *
 * strings, concatenates them together, and returns the address  *
 * of the new string. It returns NULL if there are no string     *
 * arguments, or if they are all null strings.                   */

#include <stdarg.h>         /* Include appropriate header files. */
#include <stdlib.h>
#include <string.h>

/* NSTRINGS is the maximum number of string arguments accepted (arbitrary). */

#define NSTRINGS 10

char    *concatenate (int n, ...)
{
        va_list ap;         /* Declare the argument pointer.     */

        char    *list[NSTRINGS],
                *string;
        int     index = 0,
                size = 0;

        /* Check that the number of arguments is within range.   */

        if (n <= 0)
                return NULL;
        if (n > NSTRINGS)
                n = NSTRINGS;

        va_start (ap, n);   /* Initialize the argument pointer.  */

        do {

             /* Extract the next argument and save it. */

                list[index] = va_arg (ap, char *);
                size += strlen (list[index]);
        } while (++index < n);

        va_end (ap);                     /* Terminate use of ap. */

        if (size == 0)
                return NULL;

        string = malloc (size + 1);
        string[0] = '\0';

        /* Append each argument to the end of the growing result string. */

        for (index = 0;  index < n;  ++index)
                strcat (string, list[index]);

        return string;
}


Previous Page | Next Page | Table of Contents | Index