Chapter 11

I l @ ve RuBoard

Chapter 11

  1. The initialization should include a '/0' . Also, pre-ANSI implementations don't allow you to initialize automatic arrays.

  2.  See you at the snack bar. ee you at the snack bar. See you e you 
  3.  y my mmy ummy Yummy 
  4. I read part of it all the way through.

    1. Ho Ho Ho!!oH oH oH

    2. pointer-to- char , that is, char * .

    3. The address of the initial H.

    4. *--pc means to decrement the pointer by 1 and use the value found there. --*pc means to take the value pointed to by pc and decrement that value by 1 (for example, H becomes G).

    5. Ho Ho Ho!!oH oH o

      Note

      A null character comes between ! and ! , but it produces no printing effect.


    6. while(*pc) . Check to see that pc does not point to a null character (that is, to the end of the string). The expression uses the value at the pointed-to location.

      while(pc - str) . Check to see that pc does not point to the same location that str does (the beginning of the string). The expression uses the values of the pointers themselves .

    7. After the first while loop, pc points to the null character. Upon entering the second loop, it is made to point to the storage location before the null character, that is, to the location just before the one that str points to. That byte is interpreted as a character and is printed. The pointer then backs up to the preceding byte. The terminating condition (pc == str) never occurs, and the process continues until you or the system tire.

    8. pr() must be declared in the calling program: char *pr(char *);

  5. Character variables occupy a byte, but a character constant is stored in an int , meaning the '$' typically would use 2 or 4 bytes; however, only 1 byte of the int is actually used to store the code for '$' . The string "$" uses 2 bytes: one to hold the code for '$' , and one to hold the code for '\0' .

  6. Here is what you get:

     How are ya, sweetie? How are ya, sweetie? Beat the clock. eat the clock. Beat the clock. Win a toy. Beat chat hat at t t at How are ya, sweetie? 
  7. Here is what you get:

     faavrhee *le*on*sm 
  8. Here is one solution:

     int strlen(const char * s) {   int ct = 0;   while (*s++ != ' 
      int strlen(const char * s) { int ct = 0; while (*s++ != '\0') /* or while (*s++) */ ct++; return(ct); }  
    ') /* or while (*s++) */ ct++; return(ct); }
  9. Here is one solution:

     #include <stdio.h>      /* for NULL definition          */ char * strblk(char * string) {   while (*string != ' ' && *string != ' 
      #include <stdio.h> /* for NULL definition */ char * strblk(char * string) { while (*string != ' ' && *string != '\0') string++; /* stops at first blank or null */ if (*string == '\0') return NULL; /* NULL is the null pointer */ else return string; }  
    ') string++; /* stops at first blank or null */ if (*string == '
      #include <stdio.h> /* for NULL definition */ char * strblk(char * string) { while (*string != ' ' && *string != '\0') string++; /* stops at first blank or null */ if (*string == '\0') return NULL; /* NULL is the null pointer */ else return string; }  
    ') return NULL; /* NULL is the null pointer */ else return string; }

    Here is a second solution that prevents the function from modifying the string but that allows the return value to be used to change the string. The expression (char *) string is called "casting away const ."

     #include <stdio.h>      /* for NULL definition          */ char * strblk(const char * string) {   while (*string != ' ' && *string != ' 
      #include <stdio.h> /* for NULL definition */ char * strblk(const char * string) { while (*string != ' ' && *string != '\0') string++; /* stops at first blank or null */ if (*string == '\0') return NULL; /* NULL is the null pointer */ else return (char *) string; }  
    ') string++; /* stops at first blank or null */ if (*string == '
      #include <stdio.h> /* for NULL definition */ char * strblk(const char * string) { while (*string != ' ' && *string != '\0') string++; /* stops at first blank or null */ if (*string == '\0') return NULL; /* NULL is the null pointer */ else return (char *) string; }  
    ') return NULL; /* NULL is the null pointer */ else return (char *) string; }
  10. Here is one solution:

     /* compare.c -- this will work */ #include <stdio.h> #include <string.h>   /* declares strcmp() */ #include <ctype.h> #define ANSWER "GRANT" #define MAX 40 void ToUpper(char * str); int main(void) {     char try[MAX];     puts("Who is buried in Grant's tomb?");     gets(try);     ToUpper(try);     while (strcmp(try,ANSWER) != 0)     {         puts("No, that's wrong. Try again.");         gets(try);         ToUpper(try);     }     puts("That's right!");     return 0; } void ToUpper(char * str) {     while (*str != ' 
      /* compare.c -- this will work */ #include <stdio.h> #include <string.h> /* declares strcmp() */ #include < ctype .h> #define ANSWER "GRANT" #define MAX 40 void ToUpper(char * str); int main(void) { char try[MAX]; puts("Who is buried in Grant's tomb?"); gets(try); ToUpper(try); while (strcmp(try,ANSWER) != 0) { puts("No, that's wrong. Try again."); gets(try); ToUpper(try); } puts("That's right!"); return 0; } void ToUpper(char * str) { while (*str != '\0') { *str = toupper(*str); str++; } }  
    ') { *str = toupper(*str); str++; } }
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net