Loops,like if-else statements,can be nested,on within another.the inner and outer loops need not be generated by the same type of control structure.It is essential,however,that one loop be completely embedded within on other-there can be no overlap.Each loop must be controlled by a different index.
Let us extend the lowercase to uppercase conversion programs presented in section 4.2,4.3,4.4 so that multiple lines lowercase text can be converted to uppercase,with the conversion taking place one line at a time.In other words,we will read in a line of lowercase text,display it in uppercase,then process another line,and so on.The procedure will continue until a line is detected in which the first character is asterick.
/*convert several lines of text to uppercase continue the conversion until the first character in a line is asterick(*)*/
#include stdio.h
#include ctype.h
#define EOL '\n'
main()
{
char letter[80];
int fla,count;
while((letter[0]=getchar())!='*')
{
/*read in aline of text*/
for(count=1;(letter[count]=getchar())!=EOL;++count)
;
fla=count;
/*display the line of text*/
for(count=0;count < fla;++count)
putchar(toupper(letter[count]));
printf("\n\n")
}/*end outer loop*/
printf("good bye");
}
}
A typical interactive session,illustrating the execution of the program,is shown below.Note that the input text supplied by the user is underlined
now is the time
NOW IS THE TIME
fourscore and seven years
FOURSCORE AND SEVEN YEARS
*
good bye
It should be understood that the decision to use a while statement for the outer loop and for statements for the inner loops is arbitrary.Other loop structures could have been selected.
Sunday, December 13, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment