|
|
|
|
|
|
![]() Chapter 23Answers to Selected Exercises1. [was #2; modified]
double round_nearest(double x, int n)
{
double power = pow(10.0, n);
if (x < 0.0)
return ceil(x * power - 0.5) / power;
else
return floor(x * power + 0.5) / power;
}
6. [was #6]
(a) 8. [was #8]
int numchar(const char *s, char ch)
{
int count = 0;
s = strchr(s, ch);
while (s != NULL) {
count++;
s = strchr(s + 1, ch);
}
return count;
}
10. [was #10]
if (strstr("foo#bar#baz", str) != NULL)
The assumptions are that
11. [was #12] Answers to Selected Programming Projects2. [was #4; modified]
#include <ctype.h>
#include <stdbool.h> /* C99 only */
#include <stdio.h>
int main(void)
{
bool nonblank_seen = false;
int ch;
while ((ch = getchar()) != EOF) {
if (nonblank_seen)
putchar(ch);
else if (!isspace(ch)) {
nonblank_seen = true;
putchar(ch);
}
if (ch == '\n')
nonblank_seen = false;
}
return 0;
}
Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved. |