The finalize box of the universal flowchart has nothing to do so it has been removed from the flowchart.
All that is left is translating the flowchart into C. Here is one translation:
#include <stdio.h> void main( void ) { int ch; /* current char. putchar expects an int */ int j; /* index to current char */ char sample[] = "But, soft! what light through yonder window breaks?"; j = 0; while ( sample[j] ) /* while current char is not null */ { ch = sample[j]; if ( 'a' <= ch && ch < 'z' ) { ch -= 'a' - 'A' ; /* convert to upper case */ } putchar( ch ); j++ ; } }
The condition of the while
uses the convention that anything not NULL (or zero) counts as true.
Also note that putchar()
requires an int
variable.
You might argue (correctly) that the program would be better if it used standard library functions.
If these are available you should certainly use them.
As it is, the program depends too much on knowledge of the ASCII sequence.
Using the function toupper()
would greatly improve the code:
#include <stdio.h> #include <ctype.h> void main( void ) { int j; /* index to current char */ char sample = "But, soft! what light through yonder window breaks?" j = 0; while ( sample[j] ) /* while current char is not null */ { putchar( toupper( sample[j] ); j++ ; } }
But this code actually follows the flowchart.
Parts of the chart are implemented in
toupper( int ch )
which tests if its parameter is lower case and converts it only if necessary.
It might be nice to use a for
loop instead of the while
.
Rewrite the program with a for
loop.
(Edit the following text box.)