I l @ ve RuBoard |
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 times */ putchar('\n'); } return 0; }
Listing 8.4 uses nested loops . The outer while loop gathers input characters. The first inner while loop prints leading spaces to center the text, and the second while loop prints the character multiple times. The number of characters printed is determined from the numeric value for the character. What you get depends on what you enter. If, for example, you type
What's up?
the response is this:
WWWWWWWWWWWWWWWWWWW h aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ttttttttttttttttttttttttt ''''''''''''' sssssssssssssssssssssss uuuuuuuuuuuuuuuuuuuuuuuuuuu ppppppppppppppppp ???????????????????????
What can you do with this program? You can ignore it, you can tinker with it to change the kinds of patterns it makes, or you can try to find combinations of input characters that produce a pretty pattern. For example, the input
hijklmnopqrstuiii
produces this:
h iii jjjjj kkkkkkk lllllllll mmmmmmmmmmm nnnnnnnnnnnnn ooooooooooooooo ppppppppppppppppp qqqqqqqqqqqqqqqqqqq rrrrrrrrrrrrrrrrrrrrr sssssssssssssssssssssss ttttttttttttttttttttttttt uuuuuuuuuuuuuuuuuuuuuuuuuuu iii iii iii
I l @ ve RuBoard |