Solution for Exercise 17-6

ex1706

#include <stdio.h>

int main()
{
    char input[64];
    char ch;
    int x = 0;

    printf("Type in some text: ");
    fgets(input,63,stdin);

    while(input[x] != '\n')
    {
        ch = input[x] & 223;
        putchar(ch);
        x++;
    }
    putchar('\n');

    return(0);
}

Notes

* The sample output varies between computer systems. On a PC, you may see this:

On a Mac, this could be the output:

This could be due to the character set used by the terminal. On a PC, ASCII code 0 may display as a space, whereas on a Mac it doesn't display at all. Typing in non-alpha characters has a similar effect:

The strange codes are due to the effect the bitwise-AND mask has on the character's ASCII code.

* The ASCII code for the space character is 32. If you run the solution from Exercise 17-5 using that value, here is what you see for output:

The result of the 223 AND mask on the space character is to change its code to zero.