September 1998
Intermediate to advanced
848 pages
20h 13m
English
You can use getchar() and putchar() to produce geometric patterns using characters. Listing 8.4 shows such a program. It reads a character and then prints it a number of times, the number depending on the character's ASCII value. The program also prints enough spaces to center each line.
/* patterns.c -- produces a symmetrical pattern of characters */ #include <stdio.h> int main (void) { int ch; /* read character */ int index; int chnum; while ((ch = getchar()) != '\n') { chnum = ch % 26; /* produces a number from 0 to 25 */ index = 0; while (index++ < (30 - chnum)) putchar(' '); /* spaces to center pattern */ index = 0; while (index++ < (2 * chnum + 1)) putchar(ch); /* print ch several ... |
Read now
Unlock full access