I l @ ve RuBoard |
What's wrong with this attempted declaration of a character string?
int main(void) { char name[] = {`F', `e', `s', `s' }; ... }
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; }
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; }
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; }
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!");
What is printed?
What type should x be?
What value does x get?
What does the expression *--pc mean, and how is it different from --*pc ?
What would be printed if *--pc were replaced with *pc-- ?
What do the two while expressions test for?
What happens if pr() is supplied with a null string as an argument?
What must be done in the calling function so that pr() can be used as shown?
How many bytes does `$' use? What about "$" ?
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; }
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; }
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.
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.
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 |