home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 98.img / LCNOW2.ZIP / EXAMPLES / PRINTSTR.C < prev    next >
C/C++ Source or Header  |  1988-08-02  |  958b  |  48 lines

  1. /*
  2.  * P R I N T S T R
  3.  *
  4.  * Compare the printing of strings using several
  5.  * methods, including indirect access via a pointer.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. int
  12. main(void)
  13. {
  14.     int i;          /* loop index */
  15.     int len;        /* string length */
  16.     char *cp;       /* character pointer */
  17.  
  18.     static char string[] = {
  19.         "What goes up must come down.  Ask a market analyst!\n"
  20.     };
  21.  
  22.     /*
  23.      * Print the string by using the printf() library function.
  24.      */
  25.     printf("Using printf(): ");
  26.     printf("%s", string);
  27.  
  28.     /*
  29.      * Print the string by using array indexing.
  30.      */
  31.     printf("Using an array index: ");
  32.     len = strlen(string);   /* length of string */
  33.     for (i = 0; i < len; ++i)
  34.         putchar(string[i]);
  35.  
  36.     /*
  37.      * Print the string by using a character pointer.
  38.      */
  39.     printf("Using a character pointer: ");
  40.     cp = string;            /* beginning of string */
  41.     while (*cp) {
  42.         putchar(*cp);
  43.         ++cp;
  44.     }
  45.  
  46.     return (0);
  47. }
  48.