Answer to Quiz 6(b) Thu 4 Oct 2001
1. We say that a letter lies in the first half of the alphabet
if the letter is in the range 'a'..'n' or in 'A'..'N'.
The function first_half has prototype
int first_half ( char c );
/* returns 1 if c is in the range 'a'..'n' or 'A'..'N';
returns 0 otherwise */
So first_half('g') returns 1, while first_half('r') returns 0.
Write the definition of first_half below :
int first_half ( char c )
{
if ((('a' <= c) && (c <= 'n'))
|| (('A' <= c) && (c <= 'N')))
return 1;
else
return 0;
}
2. Write a program that replaces every control character in the input by a
single space. Typically, the program is run as a.out < input.
Use iscntrl() from ctype.h.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF)
if (iscntrl(c))
putchar(' ');
else
putchar(c);
return 0;
}