Review Questions

I l @ ve RuBoard

Review Questions

  1. What's wrong with this attempted declaration of a character string?

     int main(void) {    char name[] = {`F', `e', `s', `s' };    ... } 
  2. What will this program print?

     #include <stdio.h> int main(void) {    char note[] = "See you at the snack bar.";    char *ptr;    ptr = note;    puts(ptr);    puts(++ptr);    note[7] = ` 
      #include <stdio.h> int main(void) { char note[] = "See you at the snack bar."; char *ptr; ptr = note; puts(ptr); puts(++ptr); note[7] = `\0'; puts(note); puts(++ptr); return 0; }  
    '; puts(note); puts(++ptr); return 0; }
  3. What will this program print?

     #include <stdio.h> #include <string.h> int main(void) {    char food[] = "Yummy";    char *ptr;    ptr = food + strlen(food);    while (--ptr >= food)         puts(ptr);    return 0; } 
  4. What will the following program print?

     #include <stdio.h> #include <string.h> int main(void) {     char goldwyn[40] = "art of it all ";     char samuel[40] = "I read p";     char *quote = "the way through.";     strcat(goldwyn, quote);     strcat(samuel, goldwyn);     puts(samuel);     return 0; } 
  5. Here is an exercise providing practice with strings, loops , pointers, and pointer incrementing. First, suppose you have this function definition:

     #include <stdio.h> char *pr (char *str) {   char *pc;   pc = str;   while (*pc)      putchar(*pc++);   do {      putchar(*pc);      } while (pc - str);   return (pc); } 

    Consider the following function call:

     pr("Ho Ho Ho!"); 
    1. What is printed?

    2. What type should x be?

    3. What value does x get?

    4. What does the expression *--pc mean, and how is it different from --*pc ?

    5. What would be printed if *--pc were replaced with *pc-- ?

    6. What do the two while expressions test for?

    7. What happens if pr() is supplied with a null string as an argument?

    8. What must be done in the calling function so that pr() can be used as shown?

  6. How many bytes does `$' use? What about "$" ?

  7. What does the following program print?

     #include <stdio.h> #include <string.h> #define M1  "How are ya, sweetie? " char M2[40] = "Beat the clock."; char * M3 = "chat"; int main(void) {      char words[80];      printf(M1);      puts(M1);      puts(M2);      puts(M2 + 1);      strcpy(words,M2);      strcat(words, " Win a toy.");      puts(words);      words[4] = ` 
      #include <stdio.h> #include <string.h> #define M1 "How are ya, sweetie? " char M2[40] = "Beat the clock."; char * M3 = "chat"; int main(void) { char words[80]; printf(M1); puts(M1); puts(M2); puts(M2 + 1); strcpy (words,M2); strcat(words, " Win a toy."); puts(words); words[4] = `\0'; puts(words); while (*M3) puts(M3++); puts(--M3); puts(--M3); M3 = M1; puts(M3); return 0; }  
    '; puts(words); while (*M3) puts(M3++); puts(--M3); puts(--M3); M3 = M1; puts(M3); return 0; }
  8. What does the following program print?

     #include <stdio.h> int main(void) {     static char str1[] = "gawsie";     static char str2[] = "bletonism";     char *ps;     int i = 0;     for (ps = str1; *ps != ` 
      #include <stdio.h> int main(void) { static char str1[] = "gawsie"; static char str2[] = "bletonism"; char *ps; int i = 0; for (ps = str1; *ps != `\0'; ps++) { if (*ps == `a'  *ps == `e') putchar(*ps); else (*ps)--; putchar(*ps); } putchar(`\n'); while (str2[i] != `\0') { printf("%c", i % 3 ? str2[i] : `*'); ++i; } return 0; }  
    '; ps++) { if (*ps == `a' *ps == `e') putchar(*ps); else (*ps)--; putchar(*ps); } putchar(`\n'); while (str2[i] != `
      #include <stdio.h> int main(void) { static char str1[] = "gawsie"; static char str2[] = "bletonism"; char *ps; int i = 0; for (ps = str1; *ps != `\0'; ps++) { if (*ps == `a'  *ps == `e') putchar(*ps); else (*ps)--; putchar(*ps); } putchar(`\n'); while (str2[i] != `\0') { printf("%c", i % 3 ? str2[i] : `*'); ++i; } return 0; }  
    ') { printf("%c", i % 3 ? str2[i] : `*'); ++i; } return 0; }
  9. The strlen() function takes a pointer to a string as an argument and returns the length of the string. Write your own version of this function.

  10. Design a function that takes a string pointer as argument and returns a pointer to the first space character in the string on or after the pointed-to position. Have it return a null pointer if it finds no spaces.

  11. Rewrite Listing 11.17 using ctype .h functions so that the program recognizes a correct answer regardless of the user 's choice of uppercase and lowercase.

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