September 1998
Intermediate to advanced
848 pages
20h 13m
English
You've seen how if and while statements often use relational expressions as tests. Sometimes you will find it useful to combine two or more relational expressions. For instance, suppose you want a program that counts how many times the characters other than single or double quotes appear in an input sentence. You can use logical operators to meet this need, and you can use the period character (.) to identify the end of a sentence. Listing 7.6 presents a short program illustrating the method.
#include <stdio.h> #define PERIOD `.' int main(void) { int ch; int charcount = 0; while ((ch = getchar()) != PERIOD) { if (ch != `"' && ch != `\'') charcount++; } printf("There are %d non-quote characters.\n", ... |
Read now
Unlock full access